窗口小部件setOnClickPendingIntent无法启动服务 [英] Widget setOnClickPendingIntent not starting service
问题描述
下面的例子这里我创造了我的小部件提供方便。
Following the example here i created my widget with ease.
然后添加一个按钮,我的小工具,这个按钮应该启动一个服务,所以我增加了以下code我WidgetProvider
I then added a button to my widget, this button should start a service so I added the following code to my WidgetProvider
@Override
public void onEnabled(Context context) {
Log.e("ERROR", "REMOVE ME"); // TODO remove. This is for eclipse logcat recognition
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent intent = new Intent(context, RepairService.class);
PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.widget_boost, pi);
}
在code肯定会被调用,但服务不上手。我敢肯定,我可能已经错过了一些东西与实施服务PendingIntent的,但我看不到的东西。任何人都知道吗?
The code certainly gets called, but service doesn't get started. I'm sure I have probably missed something with implementation of a service PendingIntent, but I can't see what. Anyone else know?
推荐答案
嗯,我设法解决它
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.e("ERROR", "onUpdate method called");
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
Log("Pending Intent = " + pendingIntent.toString());
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.widge, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
public static class UpdateService extends Service {
@Override
public void onCreate() {
.........
.........
}
// Service stuff here
}
}
我不完全知道什么是错的做在onEnable。也许有人可以提供一些线索,为什么。
I am not entirely sure what was wrong with doing it in onEnable. Maybe someone can shed some light as to why.
这篇关于窗口小部件setOnClickPendingIntent无法启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!