Reputation: 4807
I created app with widget that have clock, the problem is after approximately half hour the clock stop working or when i kill the app with "Advanced Task Killer" it also stop working. How can i keep my app always alive or only the widget. Here is the class of the widget:
package com.shamir;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.widget.RemoteViews;
public class Widget extends AppWidgetProvider
{
private Handler mHandler = new Handler();
RemoteViews views;
AppWidgetManager appWidgetManager;
ComponentName currentWidget;
Context context;
DateFormat time_format = new SimpleDateFormat("HH:mm:ss");
DateFormat date_format = new SimpleDateFormat("dd.MM.yy");
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
this.context = context;
this.appWidgetManager = appWidgetManager;
views = new RemoteViews(context.getPackageName(), R.layout.widget);
currentWidget = new ComponentName(context, Widget.class);
mHandler.removeCallbacks(mUpdateTask);
mHandler.postDelayed(mUpdateTask, 100);
}
final Runnable mUpdateTask = new Runnable()
{
public void run()
{
Intent informationIntent = new Intent(context,MainScreen.class);
PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 0, informationIntent, 0);
views.setOnClickPendingIntent(R.id.w_start, infoPendingIntent);
views.setTextViewText(R.id.widget_time,time_format.format(new Date()));
views.setTextViewText(R.id.widget_date,date_format.format(new Date()));
appWidgetManager.updateAppWidget(currentWidget, views);
mHandler.postDelayed(mUpdateTask, 1000);
}
};
@Override
public void onDisabled(Context context)
{
super.onDisabled(context);
mHandler.removeCallbacks(mUpdateTask);
}
}
Upvotes: 0
Views: 1918
Reputation: 429
You really shouldn't be making a clock this way. Keeping all those variables live is bad for the user, and makes it hard to recover from a system-kill. Use an alarm manager to start a service that will entirely re-draw the time every 60 seconds in a way that's independent from past drawings (i.e create calendars, etc... every time it's fired up). Here's how to start the service:
AlarmManager alman = (AlarmManager) cont.getSystemService(Context.ALARM_SERVICE);
Intent timeService = new Intent(cont, XTime.class);
PendingIntent timePending = PendingIntent.getService(cont, 0, timeService, 0);
int rem = 60 - Calendar.getInstance().get(Calendar.SECOND);
alman.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + rem * 1000, 60000, timePending);
Upvotes: 0
Reputation: 15619
I think all your questions have been answered before on SO, but to aggregate....
You cannot prevent the user from killing your app with a task killer (see this post)
To keep Android from automatically terminating your app/widget, you need a service. This makes it a lot less likely (but not impossible) for Android to kill your process when it needs the memory.
Note that keeping your widget running all the time will quickly drain the battery of your device, so be careful with this! Make sure you limit your updates (check this post for suggestions)
Upvotes: 1