我創建了應用程序與有時鐘的小工具,問題是大約半小時後停止工作時鐘或當我殺死與「高級任務殺手」應用程序它也停止工作。我怎樣才能讓我的應用永遠活着或只有小部件。這裏是類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);
}
}