点击更新小部件时,启动服务似乎不起作用。

3

我已经跟随了许多教程,在 Google 和 Stack Overflow 上搜索,并编写了以下代码以在触摸小部件时更新它:

public class WidgetService extends Service{

    @Override
    public void onStart(Intent intent, int startId) {
        Log.i("WidgetService", "Called");
        String fakeUpdate = null;
        Random random = new Random();

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
                .getApplicationContext());

        int[] appWidgetIds = intent
                .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
        if (appWidgetIds.length > 0) {
            for (int widgetId : appWidgetIds) {
                int nextInt = random.nextInt(100);
                fakeUpdate = "Random: " + String.valueOf(nextInt);
                RemoteViews remoteViews = new RemoteViews(getPackageName(),
                        R.layout.widget);
                remoteViews.setTextViewText(R.id.txt_updated, fakeUpdate);
                appWidgetManager.updateAppWidget(widgetId, remoteViews);
            }
            stopSelf();
        }
        super.onStart(intent, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

对于小部件提供者:

public class Widget extends AppWidgetProvider{

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget);
        Intent intent = new Intent(context.getApplicationContext(),
                WidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        PendingIntent pendingIntent = PendingIntent.getService(
                context.getApplicationContext(), 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.cnt_widget, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

        context.startService(intent);
    }
}

在清单文件中:

<receiver
                android:label="@string/next_trip_text"
                android:name=".widget.Widget" >
                <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>

还有XML:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:minWidth="272dp"
        android:minHeight="72dp"
        android:updatePeriodMillis="0"
        android:initialLayout="@layout/widget"
        android:configure="se.webevo.basttrafik.widget.WidgetConfigActivity" >
</appwidget-provider>

这个服务似乎没有被调用。有什么想法? :)
谢谢!

你在清单文件中做了更改吗?添加了接收器和意图过滤器吗? 然后创建元数据文件,并提供以毫秒为单位的更新时间? - Pratik Bhat
是的,清单文件正确,已添加代码,请查看编辑。 - Richard
1个回答

3

将以下内容添加到清单文件中:

  <meta-data
 android:name="android.appwidget.provider"
 android:resource="@xml/-- ur appwidgetprovider location here--">
</meta-data>

也可以尝试添加以下内容:

 <intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_ENABLED"/> 
</intent-filter>

看看它是否起作用


我在我的清单文件中有这些内容,但没有启用,我已经添加了它,但仍然没有功能 :( - Richard
我不这么认为?我该怎么做呢? :) 现在可以了,谢谢! - Richard
这会更新所有的小部件,但是有没有办法只更新被点击的小部件呢? :) - Richard
请查看这里:http://www.helloandroid.com/category/topics-covered/appwidget这也很有用:http://www.satyakomatineni.com/akc/display?url=displaynoteimpurl&ownerUserId=satya&reportId=3300 - Pratik Bhat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接