我在屏幕上有一个小部件,点击小部件的按钮启用/禁用广播接收器...但是当我稍微转动手机时...小部件上的所有值都会重置并向用户提供错误的信息...我尝试了大部分事情以防止这种情况如下:
在清单中添加 android:configChanges =“orientation | screenSize”
在mainavtivity中添加了此方法
@覆盖 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 );
}
}
我在这里缺少什么....... PLS PLSSSSSSSSSS帮助
答案 0 :(得分:0)
依赖于窗口小部件提供程序中的onUpdate
除了捕获窗口小部件的创建和删除之外,还有很大的风险。其他所有内容都应该从其他地方进行管理,通常是从onUpdate
开始的服务或线程。
然后,您可以捕获方向更改(通过各种方法,无论是来自活动还是触发服务或其他线程的广播接收器),这些方法需要重做onUpdate
中的代码。
其他地方:
答案 1 :(得分:0)
此代码中存在多个错误。
首先,您的onEnabled
和onDisabled
方法会尝试将AppWidgetProvider
视为服务。它不是:它是BroadcastReceiver
,因此这些调用将失败,可能会终止您的应用。
然而,这不会发生。您已经覆盖onReceive
而没有调用super
方法,因此根本不会调用任何其他方法。
在尝试执行其他操作之前修复这些错误。您的onEnabled
和onDisabled
方法应该为空,并且您的onReceive
应如下所示,而现在没有重复的小部件更新代码。
if (ACTION_WIDGET_ACTIVATE.equals(intent.getAction()) {
...
} else if (ACTION_WIDGET_DEACTIVATE.equals(intent.getAction()) {
...
} else {
super.onReceive(context, intent);
}
答案 2 :(得分:0)
在此处发布:After orientation change buttons on a widget are not responding您可以通过在每次更新时应用所有窗口小部件更改来避免这种情况,包括按钮点击处理程序。
答案 3 :(得分:-2)
对于AppWidgets,必须实现onUpdate
来重绘窗口小部件的内容。