问题描述
我正在尝试使用AlarmManager类创建我的第一个AppWidget,以便比30分钟更频繁地进行更新.我遵循了本教程作为设置我的小部件的基础,但对于某些我无法正确开始更新的原因.看来我从未收到任何APPWIDGET_ENABLED意图,这会触发我的AppWidgetProvider中的onEnabled事件回调.
I'm trying to create my first AppWidget using the AlarmManager class so that I can update more frequently than every 30 minutes. I followed this tutorial as a basis for setting up my widget, but for some reason I cannot get the updates to begin properly. It appears as I am never receiving any APPWIDGET_ENABLED intents, which would fire off the onEnabled event callback in my AppWidgetProvider.
这是我的AppWidgetProvider的清单定义:
Here is the manifest definition for my AppWidgetProvider:
<receiver
android:name="com.myapp.android.appwidget.MarketTimingAppWidgetProvider"
android:label="@string/appwidget_markettiming_label">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="@string/appwidget_markettiming_updateintent" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget_markettiming_info" />
</receiver>
这是我的AppWidgetProvider的代码:
Here is the code for my AppWidgetProvider:
public class MarketTimingAppWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
Log.d("myLogger", "onUpdate");
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Log.d("myLogger", "Updating Widget: " + appWidgetId);
updateWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.d("myLogger", "onEnabled running");
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 1);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
1000, createClockIntent(context));
}
public void onDisabled(Context context) {
super.onDisabled(context);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(createClockIntent(context));
}
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.d("myLogger", "Intent Received " + intent.getAction());
String widgetIntent = context.getResources().getString(R.string.appwidget_markettiming_updateintent);
// This code fires when my custom intent is received
if(widgetIntent.equals(intent.getAction())) {
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), getClass().getName());
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
for(int appWidgetId: ids) {
updateWidget(context, appWidgetManager, appWidgetId);
}
}
}
private void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_markettiming);
views.setTextViewText(R.id.widget_text, "Update: " +
DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG).format(new Date()));
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
private PendingIntent createClockIntent(Context context) {
String updateIntent = context.getResources().getString(R.string.appwidget_markettiming_updateintent);
Log.d("myLogger", "my intent: " + updateIntent);
Intent intent = new Intent(updateIntent);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
}
当我查看LogCat时,我的onReceive方法曾经收到的唯一意图是初始APPWIDGET_UPDATE意图,并且曾经执行过的唯一回调是onUpdate回调.我试过在我的appwidget意向过滤器中包含APPWIDGET_ENABLED意向(尽管文档告诉我这应该由我的小部件自动接收).没用这里只是我想念的东西吗?
When I look in LogCat the only intent that is ever recieved by my onReceive method is the initial APPWIDGET_UPDATE intent, and the only callback ever executed is the onUpdate callback. I've tried including the APPWIDGET_ENABLED intent in my appwidget intent-filter (although the docs tell me that this should be automatically received by my widget). It didn't work. Is there just something I'm missing here?
推荐答案
清单中有错误.此元素中的动作名称:
There is an error in your manifest. Action name in this element:
<action android:name="@string/appwidget_markettiming_updateintent" />
应替换为实际的字符串,而不是引用.所以应该是这样的:
should be replaced by actual string, not the reference. So it should be something like this:
<action android:name="com.myapp.android.appwidget.action.MARKETTIMING_UPDATE" />
或< string name ="appwidget_markettiming_updateintent">
元素内的values/something.xml中包含的任何内容.
or whatever you have in your values/something.xml inside the <string name="appwidget_markettiming_updateintent">
element.
问题是BroadcastReceiver无法从AlarmManager接收广播.我已经用您的代码创建了一个项目,只替换了清单中的这个字符串(当然,并向values/strings.xml中添加了适当的值),并且一切正常.
The problem is that the BroadcastReceiver does not receives the broadcasts from AlarmManager. I've created a project with your code, replaced only this string in manifest (and added the appropriate value to values/strings.xml of course) and all works fine.
此外,您可能只想用 System.currentTimeMillis()+ 1000
替换 alarmManager.setRepeating()
的第二个参数,并删除所有这些多余的Calendar-相关的东西.
In addition, you may want to replace the second parameter of alarmManager.setRepeating()
by just System.currentTimeMillis() + 1000
and remove all those extra Calendar-related stuff.
这篇关于Android AppWidget未收到APPWIDGET_ENABLED意向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!