我正在尝试调用服务类来更新我的小部件中的变量值,但它似乎从未到达服务类。我看过一些例子,但我不知道我做错了什么,而且我对服务还不是很了解。所有帮助表示赞赏。
服务等级
public class toggleMonitoringService extends Service{
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate()
{
Log.d("Me","creating service");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int startId, int something) {
// TODO Auto-generated method stub
String toggle = intent.getExtras().getString("Toggle");
Log.d("Me","Toggle : " + toggle);
if (toggle.equals("app1"))
{
UpdateWidgetService.monitorApp1 = !UpdateWidgetService.monitorApp1;
}
else if (toggle.equals("app2"))
{
UpdateWidgetService.monitorApp2 = !UpdateWidgetService.monitorApp2;
}
super.onStartCommand(intent, startId, something);
return 0;
}
}
我设置意图和待处理意图以处理来自小部件的按钮单击
Intent monitor1toggle = new Intent(this.getApplicationContext(),toggleMonitoringService.class);
monitor1toggle.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
monitor1toggle.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);
monitor1toggle.putExtra("Toggle","app1");
PendingIntent monitor1 = PendingIntent.getService(getApplicationContext(), 0 , monitor1toggle,0);
remoteViews.setOnClickPendingIntent(R.id.firstappstatus, monitor1);