0

I have an Android Service that is going to run on background, without user interaction. I would like it to starts on Android boot. It that possible? How can I debug it, since there's no LAUNCHER?

This is my AndroidManifest.xml, according to my searches:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application android:allowBackup="true"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"  >

    <service android:name=".MyService" android:exported="true" android:enabled="true" />

    <receiver android:name=".MyReceiver">
      <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED" />
          <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
  </receiver>

</application>

This is MyReceiver:

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent myService = new Intent(context, MyService.class);
        context.startService(myService);
    }
}

And MyService:

public class MyService extends IntentService {

    public MyService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        /* stuff */
    }
}

The point is it never reaches breakpoints at MyReceiver or MyService. Any ideas?

Leonardo
  • 1,263
  • 7
  • 20
  • 51
  • Question has been asked dozens of times. You need an `Activity` and that `Activity` must be start manually by the user at least once after installation. This is a security mechanism to prevent rogue apps from running on your device without your knowledge. – David Wasser Jun 10 '16 at 15:57
  • In any case, you can always debug it by launching it yourself, either via ADB or by writing a test `Activity` that starts it for you. – David Wasser Jun 10 '16 at 15:59
  • @DavidWasser This app won't be released at Play Store. So, ideally, we don't need to have an `Activity`. It seems I'll have to do a workaround and create a translucent Activity to do so :( – Leonardo Jun 10 '16 at 16:04
  • Whether or not it is in the Play Store has nothing to do with it. The user MUST launch the app manually at least once after installation. After installation the app is in the "stopped state" and it will NOT receive any broadcast `Intent`s. When the user starts the app, it will no longer be in the "stopped state" and then it will receive the broadcast `Intent`s. – David Wasser Jun 10 '16 at 16:30

0 Answers0