服务和广播接收器

时间:2013-09-23 06:48:10

标签: android service broadcastreceiver

我只是想知道是否必须声明一个在我的清单中扩展服务的类?

<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();
    }

4 个答案:

答案 0 :(得分:1)

你应该用这个:

<service android:name=.UpdateWidgetService />

你也有所改变。

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

您可以提供您的服务所在的套餐的完整路径......

谢谢!

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我发现我在服务类中编写了startCommand而不是onStartCommand! :D太傻了!谢谢你的一切