0

I have a service that I want to use for remotely managing my device. When it starts up it's supposed to connect to a server and then receive commands from there. When I start my App normally I have the MainActivity fire up my service using

startService(new Intent(this, service.class));

This works perfectly fine. However, when the autostart.class fire up the service it's reporting that it can't resolve the host. So I simply had it retry every second until it is connected but it is never able to connect even though the network connection is up and working perfectly. When I click on the App icon then, it kills the old service, starts a new one, connects and everything works great, but not with the automatic startup. It's like the service doesn't have any permissions when it's started up, but I'm not sure about that.

autostart.class

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class autostart extends BroadcastReceiver 
{
     @Override
     public void onReceive(Context context, Intent intent) {
      if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
       Intent pushIntent = new Intent(context, service.class);
       pushIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startService(pushIntent);
       Log.i("Boot Receiver", "Service started");
      }
     }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.app.rtl"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="21"
        android:targetSdkVersion="25" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:theme="@style/Theme.Transparent"
            android:name=".DialogActivity"
            android:label="@string/app_name" >
        </activity>  
        <receiver android:name=".autostart">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver> 
        <receiver
            android:name=".Darclass"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/my_admin" />

            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>
        </receiver>
        <service android:enabled="true" android:isolatedProcess="false" android:persistent="true" android:name=".service" />
    </application>
</manifest>
Flole
  • 55
  • 8
  • The receiver looks good to me. The problem may be in the service. – Edo user1419293 Nov 13 '16 at 20:49
  • @Edouser1419293 I use the isOnline function from [here](http://stackoverflow.com/questions/12752598/check-online-status-android) and it is always returning false. Also if I just use my MQTT Client library it's complaining about being unable to resolve the hostname. – Flole Nov 13 '16 at 22:50

1 Answers1

0

In general I check the connectivity with this (although getAllNetworkInfo is deprecated)

 public boolean areWeConnected(){
    /** 
     * isConnected()
     * Indicates whether network connectivity exists and it is possible to establish connections and pass data.
     * Always call this before attempting to perform data transactions.
     */
     boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected()){
                    haveConnectedWifi = true;
                    break;
                    }
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected()){
                    haveConnectedMobile = true;
                    break;
                }
        }
        return haveConnectedWifi || haveConnectedMobile;
}
Edo user1419293
  • 171
  • 2
  • 9
  • This does not help me. It never returns "true" when the service is started automatically when the system starts up. I think it is something OEM related that is behaving weird on that device. – Flole Nov 14 '16 at 18:07
  • what happens if you actually wait 1 minute after BOOT before running the service? You could add a static int counter to the autostart class and check connectivity. If not connected you could set an alarm and test connectivity after some delay. The counter would be to prevent an infinite loop. If you need more help let me know. – Edo user1419293 Nov 15 '16 at 09:51