Nowacki
Nowacki

Reputation: 19

Battery Watch / monitoring application Android

I'm working on an application to display the battery status of then phone/tablet. I have it running and working as it is, but somehow the application is 'killed' when the screen is turned off.

The strange thing is though that is is killed on my LG phone, but not on my Galaxy Tab when the screen is off/locked.

The source code is as following:

public class BatteryLevelActivity extends Activity {
    private TextView batterLevel;
    CharSequence tickerText = "No need to charge at the moment";

public void onCreate(Bundle SavedInstanceState) {
    super.onCreate(SavedInstanceState);
    setContentView(R.layout.main);
    this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(
            Intent.ACTION_BATTERY_CHANGED));
    batterLevel = (TextView) findViewById(R.id.batteryLevel);
};

private void notif(int level) {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

    int icon;

    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "BatteryWatch";
    CharSequence contentText = "Remaining charge level is " + level + "%";
    Intent notificationIntent = new Intent();
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, contentText,
            contentIntent);

    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;

    final int HELLO_ID = 1;

    mNotificationManager.notify(HELLO_ID, notification);

}

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context arg0, Intent intent) {
        int level = intent.getIntExtra("level", 0);
        batterLevel.setText("Battery level is at " + level + "%\n\n"
                + tickerText);
        notif(level);
        tickerText = "";

    }
};

}

Somehow I think that I should have a method that is called something like 'onScreenOff' or maybe 'onScreenOn' and then the application could be reloaded, but I'm open to suggestions as to how to keeo it running when the screen is off/locked.

Thanks in advance

Upvotes: 0

Views: 215

Answers (1)

D-Dᴙum
D-Dᴙum

Reputation: 7890

You probably want to consider using a Service because you want this running long term, the developer guide here explains more. Also (although I cannot find the link) want to tell Android that it should restart your app if it has to kill it. I cannot remember exactly what this is called

** Additional Info ** I gave slightly false information in the above. I believe you can make a Service re-start after Android has killed it but NOT an Activity. In the link above for the developer guide for Service it's detailed there. It's called starting a service as sticky, see the sub-section Extending the Service Class.

Upvotes: 2

Related Questions