我有一个AppWidgetProvider
应该启动一个服务,以便在App Widget上执行请求的更新。
重点是服务UpdateService
显然永远不会开始。事实上,我已经登录了onStartCommand
服务,我可以看到onStartCommand
永远不会被调用。
请注意我的申请表中有两项服务:
<service android:name="ScheduledService" />
<service android:name="UpdateService" />
非常感谢任何帮助。 感谢
这是我的相关代码:
public class AppWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context ctxt, AppWidgetManager mgr, int[] appWidgetIds) {
Intent i = new Intent(ctxt, UpdateService.class);
i.putExtra("widgetsids", appWidgetIds);
Log.e("","onUpdate di AppWidget");
// --> HERE I TRY TO START THE SERVICE <--
ctxt.startService(i);
}
public static class UpdateService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// --> onStartCommand NEVER GEST CALLED <---
Log.e("","onStartCommand di AppWidget");
int[] appWidgetIds = intent.getIntArrayExtra("widgetsids");
final int N = appWidgetIds.length;
AppWidgetManager manager = AppWidgetManager.getInstance(this);
// Perform this loop procedure for each App Widget that belongs to
// this provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews view = buildUpdate(getApplicationContext(),
appWidgetId);
// Tell the AppWidgetManager to perform an update on the current
// app widget
manager.updateAppWidget(appWidgetId, view);
}
return (START_NOT_STICKY);
}
这是我的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stocktickerwidget"
android:versionCode="1"
android:versionName="1" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<application
android:debuggable="true"
android:icon="@drawable/ic_launcher"
android:label="TestWidget" >
<receiver
android:name=".AppWidget"
android:icon="@drawable/cw"
android:label="AppWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>
<activity
android:name=".WidgetActivity"
android:theme="@android:style/Theme.NoDisplay" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="PollReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="ScheduledService" />
<service android:name="UpdateService" />
</application>
答案 0 :(得分:2)
在您的清单中,尝试包含包名称,例如:
<service android:name="com.mypackage.ScheduledService" />
<service android:name="com.mypackage.UpdateService" />
否则,请确保调用onUpdate方法。
编辑:当您将服务放在单独的文件中时,以上内容适用。由于您将其作为嵌套类包含在内,因此可以编写:
<service android:name=".AppWidget$UpdateService" />