我有一个效果很好的小部件。除非我重新启动手机,否则一切正常。如果我重新启动手机,则小部件不会显示任何数据。我在这里搜索一些答案,但我发现的所有内容都不适合我。
有人可以查看我的代码并告诉我问题在哪里?
提前致谢。
这是我的班级:
@Override
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra(WIDGET_IDS_KEY_RIGHT)) {
int[] ids = intent.getExtras().getIntArray(WIDGET_IDS_KEY_RIGHT);
if (intent.hasExtra(WIDGET_DATA_KEY_RIGHT)) {
Object data = intent.getExtras().getParcelable(WIDGET_DATA_KEY_RIGHT);
this.update(context, AppWidgetManager.getInstance(context), ids, data);
} else {
this.onUpdate(context, AppWidgetManager.getInstance(context), ids);
}
} else super.onReceive(context, intent);
if (intent.hasExtra(WIDGET_IDS_KEY_LEFT)) {
int[] ids = intent.getExtras().getIntArray(WIDGET_IDS_KEY_LEFT);
if (intent.hasExtra(WIDGET_IDS_KEY_LEFT)) {
Object data = intent.getExtras().getParcelable(WIDGET_DATA_KEY_LEFT);
this.update(context, AppWidgetManager.getInstance(context), ids, data);
} else {
this.onUpdate(context, AppWidgetManager.getInstance(context), ids);
}
} else super.onReceive(context, intent);
if (intent.hasExtra(WIDGET_IDS_KEY_CENTRAL)) {
int[] ids = intent.getExtras().getIntArray(WIDGET_IDS_KEY_CENTRAL);
if (intent.hasExtra(WIDGET_IDS_KEY_CENTRAL)) {
Object data = intent.getExtras().getParcelable(WIDGET_DATA_KEY_CENTRAL);
this.update(context, AppWidgetManager.getInstance(context), ids, data);
} else {
this.onUpdate(context, AppWidgetManager.getInstance(context), ids);
}
} else super.onReceive(context, intent);
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
startWidgetUpdateService(context);
}
protected static void startWidgetUpdateService(Context context) {
Intent serviceIntent = new Intent(context, SimpleWidgetProvider.class);
context.startService(serviceIntent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
update(context, appWidgetManager, appWidgetIds, null);
}
public void update(Context context, AppWidgetManager manager, int[] ids, Object data) {
for (int appWidgetId : ids) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
numberRight = preferences.getString(HmApplication.PREF_NUMBER_RIGHT, "");
numberCentral = preferences.getString(HmApplication.PREF_NUMBER_CENTRAL, "");
numberLeft = preferences.getString(HmApplication.PREF_NUMBER_LEFT, "");
Intent callIntentRight = new Intent(Intent.ACTION_CALL);
callIntentRight.setData(Uri.parse("tel:" + numberRight));
Intent callIntentCentral = new Intent(Intent.ACTION_CALL);
callIntentCentral.setData(Uri.parse("tel:" + numberCentral));
Intent callIntentLeft = new Intent(Intent.ACTION_CALL);
callIntentLeft.setData(Uri.parse("tel:" + numberLeft));
PendingIntent pendingIntentRight = PendingIntent.getActivity(context, 0, callIntentRight, 0);
PendingIntent pendingIntentCentral = PendingIntent.getActivity(context, 0, callIntentCentral, 0);
PendingIntent pendingIntentLeft = PendingIntent.getActivity(context, 0, callIntentLeft, 0);
preferences = PreferenceManager.getDefaultSharedPreferences(context);
isCheckedOne = preferences.getBoolean(HmApplication.PREF_CHECKBOX_ONE, false);
isCheckedTwo = preferences.getBoolean(HmApplication.PREF_CHECKBOX_TWO, false);
isCheckedThree = preferences.getBoolean(HmApplication.PREF_CHECKBOX_THREE, false);
views = new RemoteViews(context.getPackageName(), R.layout.simple_widget);
if(isCheckedOne == true){
views = new RemoteViews(context.getPackageName(), R.layout.simple_widget_one);
views.setTextViewText(R.id.actionButtonLeft, Main.textNamePhoneLeft);
}
if(isCheckedTwo == true){
views = new RemoteViews(context.getPackageName(), R.layout.simple_widget_two);
views.setTextViewText(R.id.actionButtonLeft, Main.textNamePhoneLeft);
views.setTextViewText(R.id.actionButtonCentral, Main.textNamePhoneCentral);
}
views.setOnClickPendingIntent(R.id.actionButtonRight, pendingIntentRight);
views.setOnClickPendingIntent(R.id.actionButtonCentral, pendingIntentCentral);
views.setOnClickPendingIntent(R.id.actionButtonLeft, pendingIntentLeft);
views.setTextViewText(R.id.actionButtonLeft, Main.textNamePhoneLeft);
views.setTextViewText(R.id.actionButtonCentral, Main.textNamePhoneCentral);
views.setTextViewText(R.id.actionButtonRight, Main.textNamePhoneRight);
manager.updateAppWidget(appWidgetId, views);
}
}
public void updateMyWidgets(Context context) {
AppWidgetManager man = AppWidgetManager.getInstance(context);
int[] ids = man.getAppWidgetIds(new ComponentName(context, SimpleWidgetProvider.class));
preferences = PreferenceManager.getDefaultSharedPreferences(context);
numberRight = preferences.getString(HmApplication.PREF_NUMBER_RIGHT, "");
numberCentral = preferences.getString(HmApplication.PREF_NUMBER_CENTRAL, "");
numberLeft = preferences.getString(HmApplication.PREF_NUMBER_LEFT, "");
Intent updateIntent = new Intent();
updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
updateIntent.putExtra(SimpleWidgetProvider.WIDGET_IDS_KEY_RIGHT, ids);
updateIntent.putExtra(SimpleWidgetProvider.WIDGET_DATA_KEY_RIGHT, numberRight);
updateIntent.putExtra(SimpleWidgetProvider.WIDGET_IDS_KEY_CENTRAL, ids);
updateIntent.putExtra(SimpleWidgetProvider.WIDGET_DATA_KEY_CENTRAL, numberCentral);
updateIntent.putExtra(SimpleWidgetProvider.WIDGET_IDS_KEY_LEFT, ids);
updateIntent.putExtra(SimpleWidgetProvider.WIDGET_DATA_KEY_LEFT, numberLeft);
context.sendBroadcast(updateIntent);
}
这是我的清单:
<receiver android:name=".SimpleWidgetProvider"
android:label="myWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/simple_widget_info" />
</receiver>
答案 0 :(得分:0)
实现android.intent.action.BOOT_COMPLETED
接收器和onRecieve方法调用方法updateMyWidgets
来更新小部件
您需要在清单中使用操作名称android.intent.action.BOOT_COMPLETED
定义接收器。
<!-- Start the Service if applicable on boot -->
<receiver android:name="com.test.MyReceiver ">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
确保还包括已完成的启动权限。
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
现在实施接收器
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
YourClass.updateMyWidgets();// you may need to make this method static.
}
}