I am trying to learn about service and BroadCastReceiver. Code below is a service which runs all the time in the background. The problem is I don't know how it would affect the battery consumption.
My goal is to detect the screen on and off, so I need a running service in the background when the app is close or open...
Is it going to drain a lot of battery? Can you please explain it?
Thank you
public class MyService extends Service{
private static final String TAG = "MyService";
private BroadcastReceiver mScreenOnOffReceiver;
private BroadcastReceiver OnOffBroadcastReceiver;
private NotificationManager mNotificationManager;
private Notification barNotif;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
// here to show that your service is running foreground
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent bIntent = new Intent(this, WidgetBroadCastReceiver.class);
PendingIntent pbIntent = PendingIntent.getActivity(this, 0 , bIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP);
NotificationCompat.Builder bBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("STICKY")
.setContentText("Sub Title")
.setAutoCancel(true)
.setOngoing(true)
.setContentIntent(pbIntent);
barNotif = bBuilder.build();
this.startForeground(1, barNotif);
// here the body of your service where you can arrange your reminders and send alerts
Thread mThread = new Thread() {
@Override
public void run() {
// Register the ScreenOnOffReceiver.java
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mScreenOnOffReceiver = new ScreenOnOffReceiver();
registerReceiver(mScreenOnOffReceiver, filter);
// initialize and register mScreenOnOffReceiver (no need the BroadcastReceiver class)
OnOffBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.e("", "SERVICE Screen is: " + "turned OFF.....");
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.e("", "SERVICE Screen is: " + "turned ON......");
}
}
};
registerReceiver(OnOffBroadcastReceiver, new IntentFilter(filter));
}
};
mThread.start();
return START_STICKY;
}
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "My Service has Started", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
Toast.makeText(this, "MyService Stopped", Toast.LENGTH_SHORT).show();
unregisterReceiver(mScreenOnOffReceiver);
unregisterReceiver(OnOffBroadcastReceiver);
stopForeground(true);
}
}