0

我在屏幕上有一个小部件,单击小部件的按钮启用/禁用广播接收器......但是当我稍微转动手机时......小部件上的所有值都会重置并向用户提供错误信息......我尝试了大部分防止这种情况的事情如下:

  1. 在清单中添加了android:configChanges="orientation|screenSize"

  2. 在 mainavtivity 中添加了这个方法

    @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setContentView(R.layout.widget_layout); }

  3. 我也看到了使用 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 );

  }

} 

我在这里想念什么......请帮助

4

4 回答 4

0

依靠onUpdate小部件提供程序来做更多的事情而不是捕捉小部件的创建和删除是非常冒险的。其他一切都应该从其他地方进行管理,通常是服务或线程,从onUpdate.

然后,您可以捕获方向更改(通过各种方法,无论是来自触发服务或其他线程的活动还是广播接收器),这将需要重做您在onUpdate.

其他要看的地方:

于 2013-03-31T18:57:34.713 回答
0

如在此处发布:在小部件上的方向更改按钮没有响应后,您可以通过在每次更新时应用所有小部件更改来避免这种情况,包括按钮单击处理程序。

于 2013-09-01T21:22:19.080 回答
0

这段代码有几个错误。

首先,您的onEnabledonDisabled方法尝试将其AppWidgetProvider视为服务。它不是:它是 a BroadcastReceiver,因此这些调用将失败,可能会终止您的应用程序。

然而,这不会发生。您在onReceive没有调用该super方法的情况下进行了覆盖,因此根本不会调用任何其他方法。

在尝试执行其他任何操作之前修复这些错误。您的onEnabledandonDisabled方法应该是空的,并且您onReceive应该如下所示,没有您现在拥有的重复的小部件更新代码。

if (ACTION_WIDGET_ACTIVATE.equals(intent.getAction()) {
    ...
} else if (ACTION_WIDGET_DEACTIVATE.equals(intent.getAction()) {
    ...
} else {
    super.onReceive(context, intent);
}
于 2013-07-08T23:50:22.117 回答
-2

对于 AppWidgets,实现onUpdate重绘小部件的内容是必不可少的。

于 2013-03-30T20:55:52.730 回答