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>