OnClick 与小部件中的 AlarmManager 一起对我不起作用
我有一个使用 AlarmManager 更新的小部件,它工作正常。
现在我想添加一个 onClick 事件。我的问题是我永远不会触发 onClick?或者至少由我的 onRecive 处理。
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
updateAppWidget(context, appWidgetManager, appWidgetId);
//Toast.makeText(context, "onUpdate(): " + String.valueOf(i) + " : " + String.valueOf(appWidgetId), Toast.LENGTH_LONG).show();
}
}
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId){
.
.
.
RemoteViews updateViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
updateViews.setTextViewText(R.id.widgettext, "[" + String.valueOf(appWidgetId) + "]" + strWidgetText);
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(MY_WIDGET_CLICK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,1, intent, 0);
updateViews.setOnClickPendingIntent(R.id.toggle_button_widget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
我的 onRecive 看起来像这样
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
if(MY_WIDGET_UPDATE.equals(intent.getAction())){
Bundle extras = intent.getExtras();
if(extras!=null) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), MyWidgetProvider.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
onUpdate(context, appWidgetManager, appWidgetIds);
} else if (MY_WIDGET_CLICK.equals(intent.getAction())) {
Toast.makeText(context, "onClick()", Toast.LENGTH_LONG).show();
}
Toast.makeText(context, "onReceiver()" + intent.getAction(), Toast.LENGTH_LONG).show();
}
}
正如你所看到的,我有一个 Toast,因此触发了哪个警报,我只得到 MY_WIDGET_UPDATE 作为操作
和清单
<receiver android:name="company.se.MyWidgetProvider" android:label="News Nobwidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="MY_OWN_WIDGET_UPDATE" />
<action android:name="MY_OWN_WIDGET_ONCLICK" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
<activity android:name="company.se.HelloWidgetConfig" android:label="Hello Widget Config">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="10" />
</manifest>
我对 Android 很陌生,所以非常感谢任何帮助;)
我想是我的清单过滤了 onClick 事件。有谁知道您是否可以通过 Eclipse 调试器查看触发哪些事件?
/偶来
I have a Widget that I update using a AlarmManager and it works fine.
Now I would like to add a onClick event. My problem is that I never get the onClick to fire? Or atleast being handled by my onRecive.
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
updateAppWidget(context, appWidgetManager, appWidgetId);
//Toast.makeText(context, "onUpdate(): " + String.valueOf(i) + " : " + String.valueOf(appWidgetId), Toast.LENGTH_LONG).show();
}
}
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId){
.
.
.
RemoteViews updateViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
updateViews.setTextViewText(R.id.widgettext, "[" + String.valueOf(appWidgetId) + "]" + strWidgetText);
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(MY_WIDGET_CLICK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,1, intent, 0);
updateViews.setOnClickPendingIntent(R.id.toggle_button_widget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
And my onRecive looks like this
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
if(MY_WIDGET_UPDATE.equals(intent.getAction())){
Bundle extras = intent.getExtras();
if(extras!=null) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), MyWidgetProvider.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
onUpdate(context, appWidgetManager, appWidgetIds);
} else if (MY_WIDGET_CLICK.equals(intent.getAction())) {
Toast.makeText(context, "onClick()", Toast.LENGTH_LONG).show();
}
Toast.makeText(context, "onReceiver()" + intent.getAction(), Toast.LENGTH_LONG).show();
}
}
As you can see I have a Toast so which Alarm that was fired and I only get MY_WIDGET_UPDATE as action
And the Manifest
<receiver android:name="company.se.MyWidgetProvider" android:label="News Nobwidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="MY_OWN_WIDGET_UPDATE" />
<action android:name="MY_OWN_WIDGET_ONCLICK" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
<activity android:name="company.se.HelloWidgetConfig" android:label="Hello Widget Config">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="10" />
</manifest>
I´m quite new to Android so any help is appreciated;)
I guess it´s my manifest that filters the onClick event. Does anybody know if you can see which events that fires trough Eclipse debugger?
/Olle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我真傻!
发现错误,它与 Android 没有任何关系,只是进行了糟糕的编程。
我有 else if (MY_WIDGET_CLICK.equals(intent.getAction()) 嵌套
if(extras!=null) 而不是 if(MY_WIDGET_UPDATE.equals(intent.getAction()))
所以其余代码有效!希望我没有让任何人感到困惑。
/偶来
Stupid me!
Bug found, it had nothing whith Android to do just bad programming.
I had the else if (MY_WIDGET_CLICK.equals(intent.getAction()) nested with
if(extras!=null) instead of if(MY_WIDGET_UPDATE.equals(intent.getAction()))
So the rest of the code works! Hope I didn´t confuse anyone.
/Olle