【发布时间】:2013-06-26 07:48:20
【问题描述】:
这里是完整的android开发新手,请注意。
我正在尝试开发一个主屏幕小部件,它允许您点击小部件来拨打预定义的号码。我可以创建小部件并将其添加到主屏幕,但是,当我尝试使用 setOnClickPendingIntent 使小部件可点击时,它不会启动活动。我正在使用的代码如下:
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.callwidget_layout);
Log.d("stuff", "remoteview defined");
Intent callIntent = new Intent(Intent.ACTION_CALL);
Log.d("stuff", "intent created");
callIntent.setData(Uri.parse("tel:"+8888888));
Log.d("stuff", "intent data added");
PendingIntent clickPI=PendingIntent
.getBroadcast(context, 0,
callIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Log.d("stuff", "pending intent created");
remoteViews.setOnClickPendingIntent(R.layout.callwidget_layout, clickPI);
Log.d("stuff", "setonclickpendingintent created");
}
Log.d 方法工作正常,因为它们显示在 logcat 输出中,但点击小部件没有任何作用。 logcat 上没有显示其他错误消息。是不是我做错了什么?
更新:将 setOnClickPendingIntent 更改为引用 ID 为“callbutton” (remoteViews.setOnClickPendingIntent(R.id.callbutton, clickPI);) 的按钮,并尝试将这三行代码添加到 onUpdate 方法中:
ComponentName myWidget = new ComponentName(context, WidgetProvider.class);
appWidgetManager.getInstance(context).updateAppWidget(myWidget, remoteViews);
Log.d("stuff", "widget updated");
再次,Log.d 方法有效,提示小部件更新正常,但点击按钮仍然没有任何作用。
更新 2: 将 PendingIntent clickPI=PendingIntent.getBroadcast 更改为 PendingIntent clickPI=PendingIntent.getActivity 也没有任何作用。
【问题讨论】:
标签: android android-widget android-pendingintent