2013-03-17 197 views
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> 

+1

依我之見,'UpdateService'是一個嵌套類AppWidget'的',所以應該在'AndroidManifest.xml'這樣'聲明' – 2013-03-17 15:54:18

回答

2

在您的清單,嘗試包括包名,如:

<service android:name="com.mypackage.ScheduledService" /> 

<service android:name="com.mypackage.UpdateService" /> 

否則,請確保您的onUpdate方法被調用。

編輯:當你有你的服務在一個單獨的文件上面適用。既然你包括它作爲一個嵌套類,你可以這樣寫:

<service android:name=".AppWidget$UpdateService" /> 
+0

謝謝你解決了這個問題 – 2013-03-17 16:35:42