AppWidgetProvider问题
我有一个 AppWidgetProvider,当小部件首次添加到主屏幕时,我需要进行一些初始化。据我所知,执行此操作的位置是在 onEnabled(Context context) 方法中。我的问题是这个方法永远不会被调用(据我在 logcat 中看到的)。
这是我的代码:
public class MyMonitorWidget extends AppWidgetProvider{
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.v("LOG", "Widget onEnabled");
Intent intentToFire = new Intent(UpdateAlarmReceiver.ACTION_UPDATE_ALARM);
context.sendBroadcast(intentToFire);
}
...
}
和我的 appwidget-provider xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minWidth="146dp"
android:minHeight="74dp"
android:label="Monitor Widget"
/>
以及清单中:
<receiver android:name="MyMonitorWidget" android:label="Monitor Widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="com.android.myMonitor.ACTION_NOTIFY_WIDGET"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/monitor_widget_info"/>
</receiver>
您认为问题是什么?
I have an AppWidgetProvider, and I need to do some initialization when a widget is first added to the home screen. I understand that the place to do it is in the onEnabled(Context context) method. My Problem is that this method is never called (As far as I can see in the logcat).
Here is my code:
public class MyMonitorWidget extends AppWidgetProvider{
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.v("LOG", "Widget onEnabled");
Intent intentToFire = new Intent(UpdateAlarmReceiver.ACTION_UPDATE_ALARM);
context.sendBroadcast(intentToFire);
}
...
}
And my appwidget-provider xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minWidth="146dp"
android:minHeight="74dp"
android:label="Monitor Widget"
/>
and in the manifest:
<receiver android:name="MyMonitorWidget" android:label="Monitor Widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="com.android.myMonitor.ACTION_NOTIFY_WIDGET"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/monitor_widget_info"/>
</receiver>
What do you think is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要添加
android.appwidget.action.APPWIDGET_ENABLED
作为另一个操作:否则,您将不会收到触发
onEnabled()
的广播。You need to add
android.appwidget.action.APPWIDGET_ENABLED
as another action:Without that, you will not receive the broadcast that triggers
onEnabled()
.不要忘记
android:exported
属性!当android:exported
为false
时,我没有收到onDelete()
Don't forget about
android:exported
property! I didn't receiveonDelete()
whenandroid:exported
wasfalse