2013-03-17 123 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