1

我是 Android 开发的新手。我已经设法编写了一个使用警报管理器每 30 分钟更新一次的小部件。这部分工作正常。

我现在还想在我点击它时手动更新小部件(即通过广播触发相同的 OnReceive(),无论是通过重复警报还是点击)。

我希望它就像在末尾添加两行一样简单(如下所示),但是当我单击小部件时 onClick 不起作用。实现我想要做的最好的方法是什么?

@Override
public void onEnabled(Context context) {
    super.onEnabled(context);
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    //Update after 30 minutes
    am.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis()+ 300, 18000000 , pi);

        //Update manually by Clicking widget (this part not working)    
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.my_widget_layout);         
    remoteViews.setOnClickPendingIntent(R.layout.my_widget_layout, pi);
4

1 回答 1

1

你在你的代码中混合R.layout.my_widget_layout和。R.id.my_widget_layout

应该注意,AndroidR.id.xyz对所有生成的小部件项使用引用的 ID,而R.layout.abc对于您的视图(布局)

我预计该错误是在线提出的:

remoteViews.setOnClickPendingIntent(R.id.my_widget_layout, pi);
于 2012-08-31T02:16:30.910 回答