Widget 中可点击的 ImageView?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试更改此行
:
Try changing this line:
to
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