2013-09-23 39 views
0

我只想知道是否必须声明一个扩展我的清单中的服务的类?服务和广播接收器

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <receiver android:name="com.cherise.readerwidget.MyWidgetProvider" > 
     <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.appwidget.provider" 
      android:resource="@xml/widget_info" /> 
    </receiver> 

    <service 
     android:name="com.cherise.readerwidget.UpdateWidgetService" 
     android:enabled="true" 
     android:exported="true" > 
    </service> 
</application> 

在这个类中,我有一个做HttpConnections的AsyncTask。

它似乎没有更新我的部件在后执行。

请帮

 protected void onPostExecute(String details) 
    { 
     for(int widgetId : allWidgetIds) 
     { 
      RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget_layout); 
      //remoteViews.setTextViewText(R.id.update, news.get(0).getTitle()); 
      remoteViews.setTextViewText(R.id.update, "Bloemfontein: " + details + " F"); 

      Intent clickIntent = new Intent(getApplicationContext(), MyWidgetProvider.class); 
      clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
      clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds); 

      PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent); 
      appWidgetManager.updateAppWidget(widgetId, remoteViews); 
     } 
     stopSelf(); 
    } 
+0

您的BroadcastReceiver是否已注册?请发布您的清单文件。 – RvdK

+0

检查onPostExecute是否被调用... –

+0

UpdateNews un = new UpdateNews(); un.execute(); – Cherise

回答

0

是的,你需要在服务标签的清单文件中声明。

+0

你能告诉我如何? – Cherise

+0

这应该在您的标记下。请确保您的服务路径正确。 – prijupaul

1

你应该使用这样的:

<service android:name=.UpdateWidgetService /> 

你有这也改变。

<service android:name= yourpackegename.UpdateWidgetService android:enabled="true"/> 

你可以给包的完整路径,其中所谓的为您服务...

谢谢!

+0

我这样做了,但它仍未更新我的小部件。可能是什么问题? – Cherise

+0

..如果服务在根包里 – Egor

+0

@Cherise chk更新了答案.. – Piyush

0
<service 
     android:name="UpdateWidgetService" 
     android:enabled="true" 
     android:exported="true" > 
    </service> 
+0

试过这个,没有区别! – Cherise

0

我想到我在我的服务类中写了startCommand而不是onStartCommand! :D太愚蠢了!感谢您的一切