我正在android中创建一个主屏幕小部件。窗口小部件提供程序启动服务,该服务将每5分钟更新一次窗口小部件。当我从主屏幕删除小部件时,我取消了小部件提供程序的'onDisabled()'方法中的服务。但是服务是null,服务永远在后台运行!我不知道为什么这是空的。有关如何做到这一点的任何想法?我也可以为所有小部件实例提供单个服务实例吗?
我的小部件提供商
public class MyWidgetProvider extends AppWidgetProvider {
private PendingIntent service = null;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final AlarmManager m = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
final Calendar TIME = Calendar.getInstance();
TIME.set(Calendar.MINUTE, 0);
TIME.set(Calendar.SECOND, 0);
TIME.set(Calendar.MILLISECOND, 0);
final Intent i = new Intent(context, UpdatorService.class);
i.putExtra("WIDGET_IDS", appWidgetIds);
if (service == null) {
service = PendingIntent.getService(context, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
}
m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(), 60 * 1000,
service);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
final AlarmManager m = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Log.d("onDisabled() >>> ", service + "");
if (service != null) {
Log.d("WIDGET>>>", "Cancelling the service");
m.cancel(service);
service.cancel();
}
}
}
UpdatorService
public class UpdatorService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int[] widgetIds = (int[]) intent.getExtras().get("WIDGET_IDS");
for (int i = 0; i < widgetIds.length; i++) {
SharedPreferences prefs = getSharedPreferences("PREFS_"
+ widgetIds[i], Context.MODE_PRIVATE);
if (prefs != null) {
//something logic here
//This is a async task to get http data
new RequestTask(this, parameterMap, widgetIds[i]).execute();
}
}
return START_NOT_STICKY;
}
private void buildUpdate(int widgetId, String result) {
ChartRenderer ch = new ChartRenderer();
GraphicalView gview=ch.generateChart(result, this);
RemoteViews views = new RemoteViews(getPackageName(),
R.layout.bw_widget_layout);
views.setBitmap(R.id.chart, "setImageBitmap", ch.getBitmapFromView(gview));
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetManager.updateAppWidget(widgetId, views);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void processResult(String result, int widgetId) {
buildUpdate(widgetId, result);
}
}
RequestTask
class RequestTask extends AsyncTask<String, String, String> {
private UpdatorService service;
private Map<String, String> paramMap;
int widgetId;
RequestTask(UpdatorService service, Map<String, String> paramMap, int wid) {
this.service = service;
this.paramMap = paramMap;
widgetId = wid;
}
@Override
protected String doInBackground(String... uri) {
.......
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
service.processResult(result, widgetId);
}
}
答案 0 :(得分:1)
首先,您不应该将服务传递给您的RequestTask,即糟糕的设计。
如果您查看他们遵循相同行为的示例窗口小部件,您应该使用Intent向您的提供程序发送更新。此外,您的服务写得不好,您应该在完成工作后立即停止立即,而不是从应用程序停止它,它更有效,更清洁服务状态维护。
服务的整个概念就是这样(在你的情况下):
做好工作然后停止
Intent请求启动服务进行处理,通过Intent Extras传递信息,然后让小部件发挥其魔力,然后自行停止,负责任地调用<service>.stopSelf()
。