1

I'm new to Android development, and I'm trying to create a simple "proof of concept application" which will run as a background service. I'm trying to use IntentService with a BroadcastReceiver to kick off the process (during boot time for now, at some point I might switch it to Screen on / user present).

I created a new project within Android Studio with no activity. Then I added the following Java files and made the following changes to the AndroidManifest.xml.

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.circlesquires.netcountable.netcountable">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service android:name=".SnapshotService" android:exported="true">
        </service>
        <receiver android:name=".ServiceStarter" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

ServiceStarter.java

package com.circlesquires.netcountable.netcountable;

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

/**
 * Created by camha on 6/18/2016.
 */
public class ServiceStarter extends BroadcastReceiver{
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("output", "onReceive occured!");

        if(intent.getAction().equals(ACTION)) {
            Intent serviceIntent = new Intent(context, SnapshotService.class);

            context.startService(serviceIntent);
        }
    }
}

SnapshotService.java

package com.circlesquires.netcountable.netcountable;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

/**
 * Created by camha on 6/18/2016.
 */
public class SnapshotService extends IntentService {

    public SnapshotService() {
        super("SnapshotService");
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {
        while(true) {

            Log.i("output", "I'm running!");

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

I deploy the application to an emulator being sure to click the "debug" button. However I don't ever see ANY of the logs outputted in logcat.

I'm sure I'm doing something wrong--help me figure out what :D. Thanks!

CamHart
  • 3,825
  • 7
  • 33
  • 69
  • Your app needs to have an `Activity` that you launch at least once after installation to bring it out of the _stopped_ state. Until then, your boot Receiver will not be delivered the broadcast (since API 3.1). – Mike M. Jun 19 '16 at 21:40
  • Okay so if I add an activity, it just needs to be ran once (no matter if the phone restarts etc?) – CamHart Jun 19 '16 at 21:55
  • Yeah, it basically needs to be run once after installation. However, it can be put back into the _stopped_ state - e.g., if the user forcibly stops your app from Settings - in which case they'd have to run it again for your boot Receiver to work. Simply rebooting, though, won't put it back into the _stopped_ state. – Mike M. Jun 19 '16 at 22:02

2 Answers2

0

android.intent.action.BOOT_COMPLETED is send, as its name suggest, after, but only after a device boot time. Are you restarting your device?

Instead of restarting device to test out your application, you can send system broadcasts directly through adb:

./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n <your.package.name>/.<YourReceiverClass>

You can find more info here.

Community
  • 1
  • 1
skywall
  • 3,956
  • 1
  • 34
  • 52
0
 if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        Intent i = new Intent(context, SnapshotService.class);
        context.startService(i);
    }

compare direct with intent value instead of making string and one thing more

On Android 3.1 and higher, the user must launch one of your activities before any manifest-registered BroadcastReceiver will work. check this https://stackoverflow.com/a/17067671/3288890

Community
  • 1
  • 1
Adiii
  • 54,482
  • 7
  • 145
  • 148