简体   繁体   中英

Service stops running after some time

I'm implementing an app that requires to monitor the device's battery level. So I implemented a Service like this:

public class BatteryService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        return super.onStartCommand(intent, flags, startId);
    }

    private final BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Save the data
        }
    };
}

I start this service in my MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService(new Intent(this, BatteryService.class));
    }
}

and in a android.intent.action.BOOT_COMPLETED receiver:

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context,BatteryService.class));
    }
}

I have two problems:

  • Testing app after some time, it stops registering battery changed events
  • After rebooting the phone the app crashes with the following error

Caused by java.lang.IllegalStateException: Not allowed to start service Intent {...} app is in background

Maybe my questions are self-related:

  1. How do I avoid the service to stop running after some time?
  2. How do I avoid the crash on boot? I have read that using "startForegroundService" but, it does require to present a notification to the user.
  3. How can I run in background and monitor the battery properly without constantly showing a notification?

Thanks!

Actually it was because of Android built-in security. They discouraged background services for user's protection. To allow such action, user must be aware that your service is running by starting it in foreground.

Intent intent = new Intent(this, typeof(SomeActivityInYourApp));
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,   
PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setSmallIcon(R.Drawable.my_icon);
builder.setTicker("App info string");
builder.setContentTitle("Hey there");
builder.setContentText("My battery service is running!")
builder.setContentIntent(pi);
builder.setOngoing(true);

Notification notification = builder.build();

startForeground(SERVICE_NOTIFICATION_ID, notification);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM