更新小部件 onClick,启动服务似乎不起作用

发布于 2024-12-16 00:32:03 字数 3008 浏览 1 评论 0原文

我已经遵循了一堆教程,在谷歌和堆栈溢出上搜索,并想出了这个代码来在我触摸它时更新我的​​小部件:

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>

该服务似乎不是根本没有打电话。有什么想法吗? :)

谢谢!

I've followed a bunch of tutorials, search on google and on stack overflow and came up with this code to update my widget when i touch it:

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

And for widget provider:

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

In manifest:

<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>

And 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>

The service doesn't seem to be called at all. Any ideas? :)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅黛梨妆こ 2024-12-23 00:32:03

将其添加到清单:

  <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>

看看它是否有效

add this to manifest:

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

also try adding:

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

see if it works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文