我在屏幕上有一个小部件,单击小部件的按钮启用/禁用广播接收器......但是当我稍微转动手机时......小部件上的所有值都会重置并向用户提供错误信息......我尝试了大部分防止这种情况的事情如下:
在清单中添加了android:configChanges="orientation|screenSize"
在 mainavtivity 中添加了这个方法
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setContentView(R.layout.widget_layout); }
我也看到了使用 onSaveInstanceState 和所有的建议,但是我无法从远程视图方法从我的小部件中获取 textview 数据来保存它..任何其他方式
---- 这是代码 ----------
在清单...
<receiver android:name="MyWidgetProvider" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.makelifesimple.autopickmobiletracker.MyWidgetProvider.ACTION_WIDGET_ACTIVATE"/>
<action android:name="com.makelifesimple.autopickmobiletracker.MyWidgetProvider.ACTION_WIDGET_DEACTIVATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
<activity
android:name="com.makelifesimple.autopickmobiletracker.MainActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
--- 在 MyWidgetProvider 中 ---
public class MyWidgetProvider extends AppWidgetProvider {
public static String ACTION_WIDGET_ACTIVATE = "ActivatePickup";
public static String ACTION_WIDGET_DEACTIVATE = "DeactivatePickup";
RemoteViews remoteViews;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
Toast.makeText(context, "in onupdate", Toast.LENGTH_SHORT).show();
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent active = new Intent(context, MyWidgetProvider.class);
active.setAction(ACTION_WIDGET_ACTIVATE);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.button1, actionPendingIntent);
active = new Intent(context, MyWidgetProvider.class);
active.setAction(ACTION_WIDGET_DEACTIVATE);
actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.button2, actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onEnabled(Context context){
Toast.makeText(context, "in enables", Toast.LENGTH_SHORT).show();
context.startService(new Intent(context, MyWidgetProvider.class));
}
@Override
public void onDisabled(Context context){
Toast.makeText(context, "in disable", Toast.LENGTH_SHORT).show();
context.stopService(new Intent(context, MyWidgetProvider.class));
}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "in onRecive", Toast.LENGTH_SHORT).show();
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
if (intent.getAction().equals(ACTION_WIDGET_ACTIVATE)) {
ComponentName receiver = new ComponentName(context, PhoneCallReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
//remoteViews.setTextViewText(R.id.button1,"ACTIVATED");
remoteViews.setTextViewText(R.id.textView2,"ACTIVE");
} else if (intent.getAction().equals(ACTION_WIDGET_DEACTIVATE)) {
ComponentName receiver = new ComponentName(context, PhoneCallReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
remoteViews.setTextViewText(R.id.textView2,"INACTIVE");
Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
headSetUnPluggedintent.putExtra("state", 0); // 0 = unplugged 1 = Headset with microphone 2 = Headset without microphone
headSetUnPluggedintent.putExtra("name", "Headset");
context.sendOrderedBroadcast(headSetUnPluggedintent, null);
}
Intent active = new Intent(context, MyWidgetProvider.class);
active.setAction(ACTION_WIDGET_ACTIVATE);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.button1, actionPendingIntent);
active = new Intent(context, MyWidgetProvider.class);
active.setAction(ACTION_WIDGET_DEACTIVATE);
actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.button2, actionPendingIntent);
ComponentName thisWidget = new ComponentName( context, MyWidgetProvider.class );
AppWidgetManager.getInstance( context ).updateAppWidget(thisWidget, remoteViews );
}
}
我在这里想念什么......请帮助