Widget 中可点击的 ImageView?

发布于 2025-01-08 05:39:23 字数 3036 浏览 0 评论 0原文

我在 android 中有这个小部件,它显示图像并由广播接收器定期更新。现在我想让用户能够通过单击小部件来启动配置屏幕(首选项活动)。这里是:

小部件提供程序类:

public class MyProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        ComponentName thisWidget = new ComponentName(context.getApplicationContext(), MyProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        for (int widgetId : appWidgetIds) {
            // setting onClickListener - at least i TRY to
            Intent onClickIntent = new Intent(context, Preferences.class);

            PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onClickIntent, 0);
            rViews.setOnClickPendingIntent(R.id.graph, onClickPendingIntent);

            appWidgetManager.updateAppWidget(widgetId, rViews);
        }
    }
}

小部件的更新从这里触发:

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());

        int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
        ComponentName myWidget = new ComponentName(context.getApplicationContext(), MyProvider.class);
        int[] allMyWidgetIds = appWidgetManager.getAppWidgetIds(myWidget);  

        for (int widgetId : allMyWidgetIds)
        {
            RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            // update the widgets content, i.e. picture
            appWidgetManager.updateAppWidget(widgetId, rViews);
        }

        // schedule next update
        Calendar c = Calendar.getInstance();
        c.add(Calendar.SECOND, Config.UPDATE_RATE);
        Date d = new Date(c.getTimeInMillis());

        Intent timerIntent = new Intent(context.getApplicationContext(), TimeIntervalReceiver.class);

        timerIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
        PendingIntent timerPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, timerIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager aManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        aManager.set(AlarmManager.RTC, d.getTime(), timerPendingIntent);
    }
}

顺便提一下: Preference.java-Activity 已在清单中注册并且可以正确启动(我通过将其设置为启动来验证这一点)活动)。 无论如何,onclick 功能根本不起作用:( 我还尝试将代码移动到更新小部件的“MyReceiver”类:什么都没有:(

我已经看过了: * Android:可点击的图像视图小部件 * http://www.vogella.de/articles/AndroidWidgets/article.html

然而,这根本没有帮助:(

这里有人能告诉我我的问题出在哪里吗?

I have this widget in android, that displays an image and gets updated periodically by a broadcast receiver. Now I want to give the user the ability to start a configuration screen (preference activity) by clicking on the widget. Here it goes:

The widget provider class:

public class MyProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        ComponentName thisWidget = new ComponentName(context.getApplicationContext(), MyProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        for (int widgetId : appWidgetIds) {
            // setting onClickListener - at least i TRY to
            Intent onClickIntent = new Intent(context, Preferences.class);

            PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onClickIntent, 0);
            rViews.setOnClickPendingIntent(R.id.graph, onClickPendingIntent);

            appWidgetManager.updateAppWidget(widgetId, rViews);
        }
    }
}

The updates of the widget are triggered from here:

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());

        int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
        ComponentName myWidget = new ComponentName(context.getApplicationContext(), MyProvider.class);
        int[] allMyWidgetIds = appWidgetManager.getAppWidgetIds(myWidget);  

        for (int widgetId : allMyWidgetIds)
        {
            RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            // update the widgets content, i.e. picture
            appWidgetManager.updateAppWidget(widgetId, rViews);
        }

        // schedule next update
        Calendar c = Calendar.getInstance();
        c.add(Calendar.SECOND, Config.UPDATE_RATE);
        Date d = new Date(c.getTimeInMillis());

        Intent timerIntent = new Intent(context.getApplicationContext(), TimeIntervalReceiver.class);

        timerIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
        PendingIntent timerPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, timerIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager aManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        aManager.set(AlarmManager.RTC, d.getTime(), timerPendingIntent);
    }
}

Just to mention it: the Preference.java-Activity is registered in the Manifest and can be started correctly (I verified this by setting it as the startup activity).
Anyhow, the onclick-functionality does not work at all :(
I also tried to move the code to the "MyReceiver"-class which updates the widgets: nothing :(

I already looked at:
* Android: Clickable imageview widget
* http://www.vogella.de/articles/AndroidWidgets/article.html

However, this didn't help at all :(

anyone here who can tell me where my problem is?

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

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

发布评论

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

评论(2

梦屿孤独相伴 2025-01-15 05:39:23

尝试更改此行

PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onClickIntent, 0);

PendingIntent onClickPendingIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0);

Try changing this line:

PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onClickIntent, 0);

to

PendingIntent onClickPendingIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0);
生寂 2025-01-15 05:39:23

PendingIntent onClickPendingIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0);

检索将启动新活动的 PendingIntent

http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context, int, android.content.Intent, int)

请参考完整参考链接

PendingIntent onClickPendingIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0);

Retrieve a PendingIntent that will start a new activity

http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context, int, android.content.Intent, int)

Plz refer link for full reference

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