0
我有一個AppWidgetProvider
應該觸發一個服務,以便在App Widget上執行所請求的更新。Android:爲什麼服務無法啓動?
重點是服務顯然永遠不會開始。事實上,我已經登錄了服務的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>
依我之見,'UpdateService'是一個嵌套類AppWidget'的',所以應該在'AndroidManifest.xml'這樣'聲明 ' –
2013-03-17 15:54:18