1

我开发了一个应用程序,它有小部件,这些都运行良好,但是我改变了设备的方向,它显示得很好。但是有时当我更改方向数据时不会显示。任何人都可以帮助我,为什么这个问题会在某个时间发生。这是代码

 @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
            super.onUpdate(context, appWidgetManager, appWidgetIds);
            Logger.d(TAG, "WidgetProvider ENTER onUpdate");
            String str = null;
            SharedPreferences prefs = context.getSharedPreferences("userInformation", Context.MODE_PRIVATE);
            if (prefs != null)
{
                str = prefs.getString("ImageURL", "").toString();
            }
            if (str != null && str != "") {
                Logger.d(TAG, "WidgetProvider (Account logined)");

                for (int j = 0; j < appWidgetIds.length; j++) {

                    Intent intent = new Intent(context, WidgetService.class);
                    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[j]);
                    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

                    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.facedroid_widget_layout);
                    remoteViews.setRemoteAdapter(appWidgetIds[j], R.id.listTasks, intent);

                    remoteViews.setViewVisibility(R.id.rl_complete_widget, View.GONE);
                    // The empty view is displayed when the collection has no items.
                    // It
                    // should be a sibling
                    // of the collection view.

                    /******** Click Listener for All external items ***********/
                    // Register an onClickListener
                    Intent clickIntent = new Intent(context, WidgetProvider.class);

                    clickIntent.setAction(CLICK_ACTION_FACEDROID_LOGO_BUTTON);
                    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
                    PendingIntent pendingIntent_facedroid_logo = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    remoteViews.setOnClickPendingIntent(R.id.rl_topManagerBar_logo, pendingIntent_facedroid_logo);

                    clickIntent.setAction(CLICK_ACTION_COMMENT_BUTTON);
                    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
                    PendingIntent pendingIntent_commentBtn = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    remoteViews.setOnClickPendingIntent(R.id.commentBtn, pendingIntent_commentBtn);

                    clickIntent.setAction(CLICK_ACTION_CAMERA_BUTTON);
                    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
                    PendingIntent pendingIntent_cameraBtn = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    remoteViews.setOnClickPendingIntent(R.id.cameraBtn, pendingIntent_cameraBtn);

                    clickIntent.setAction(CLICK_ACTION_CHECKIN_BUTTON);
                    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
                    PendingIntent pendingIntent_checkinBtn = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    remoteViews.setOnClickPendingIntent(R.id.checkinBtn, pendingIntent_checkinBtn);

                    clickIntent.setAction(CLICK_ACTION_REFRESH_BUTTON);
                    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
                    PendingIntent pendingIntent_refreshBtn = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    remoteViews.setOnClickPendingIntent(R.id.refreshBtn, pendingIntent_refreshBtn);
                    /******** Click Listener for All external items ***********/

                    clickIntent.setAction(CLICK_ACTION_ITEM_CLICK);
                    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[j]);
                    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
                    PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    remoteViews.setPendingIntentTemplate(R.id.listTasks, toastPendingIntent);

                    appWidgetManager.updateAppWidget(appWidgetIds[j], remoteViews);
                }

            } else {
                Logger.d(TAG, "WidgetProvider (Account not logined)");

                for (int j = 0; j < appWidgetIds.length; j++) {

                    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.facedroid_widget_layout);
                    /******** Click Listener for All external items ***********/
                    // Register an onClickListener
                    Intent clickIntent = new Intent(context, WidgetProvider.class);

                    clickIntent.setAction(CLICK_ACTION_COMPLETE_WIDGET_CLICK);
                    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
                    PendingIntent pendingIntent_widget_top_bar = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    remoteViews.setOnClickPendingIntent(R.id.rl_complete_widget, pendingIntent_widget_top_bar);

                    appWidgetManager.updateAppWidget(appWidgetIds[j], remoteViews);
                }
            }

            Logger.d(TAG, "WidgetProvider EXIT onUpdate");
    //      super.onUpdate(context, appWidgetManager, appWidgetIds);
        }

// 这里是 Onreceive 方法的代码

@Override
    public void onReceive(final Context context, Intent main_intent) {

        super.onReceive(context, main_intent);
        Logger.d(TAG, "WidgetProvider ENTER onReceive");

        // all the intents get handled by this method
        // mainly used to handle self created intents, which are not
        // handled by any other method

        // the super call delegates the action to the other methods

        // for example the APPWIDGET_UPDATE intent arrives here first
        // and the super call executes the onUpdate in this case
        // so it is even possible to handle the functionality of the
        // other methods here
        // or if you don't call super you can overwrite the standard
        // flow of intent handling
        String str = null;
        SharedPreferences prefs = context.getSharedPreferences("userInformation", Context.MODE_PRIVATE);
        if (prefs != null) {
            str = prefs.getString("ImageURL", "").toString();
        }
        if (str != null && str != "") {
            Logger.d(TAG, "WidgetProvider (Account logined)");

            if (main_intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
                Logger.d(TAG, "WidgetProvider UpdateAction");
                String updateType = main_intent.getStringExtra("updateType");
                Logger.d(TAG, "updateType=" + updateType);
                if (updateType != null) {
                    if (updateType.equals("single")) {
                        final int appWidgetId = main_intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
                        final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                        updateAppWidgetSingle(context, appWidgetManager, appWidgetId);
                    } else if (updateType.equals("multiple")) {
                        final int[] appWidgetIds = (int[]) main_intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                        final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                        updateAppWidgetMultiple(context, appWidgetManager, appWidgetIds);
                    }
                }
            } else if (main_intent.getAction().equals(CLICK_ACTION_ITEM_CLICK)) {
                Logger.d(TAG, "WidgetProvider ItemClick");
                // int appWidgetId =
                // main_intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                // AppWidgetManager.INVALID_APPWIDGET_ID);
                int viewIndex = main_intent.getIntExtra(EXTRA_ITEM, 0);

                LinkInfo info_s = null;
                try {
                    info_s = public_feed.DATA.get(viewIndex);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (e != null) {
                        Log.e(TAG, "onReceive() " + e.getMessage().toString());
                    } else {
                        Log.e(TAG, "onReceive() e=null onReceive");
                    }
                }

                if (info_s != null) {
                    Intent i = new Intent(context.getApplicationContext(), SinglePostActivityWidget.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    i.putExtra("LinkInfo", info_s);
                    context.getApplicationContext().startActivity(i);
                }

            } else if (main_intent.getAction().equals(CLICK_ACTION_FACEDROID_LOGO_BUTTON)) {
                Logger.d(TAG, "WidgetProvider FacedroidLogoButtonClicked");

                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                ComponentName component_name = new ComponentName("com.platinumapps.facedroid", "com.platinumapps.activities.Splash_Activity");
                intent.setComponent(component_name);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                if (intent != null) {
                    context.startActivity(intent);
                }

            } else if (main_intent.getAction().equals(CLICK_ACTION_COMMENT_BUTTON)) {
                Logger.d(TAG, "WidgetProvider CommentButtonClicked");

                Intent i = new Intent(context.getApplicationContext(), StatusShareICS.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.getApplicationContext().startActivity(i);

            } else if (main_intent.getAction().equals(CLICK_ACTION_CAMERA_BUTTON)) {
                Logger.d(TAG, "WidgetProvider CameraButtonClicked");

                Intent i = new Intent(context.getApplicationContext(), PhotoSharePopup_widget.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.getApplicationContext().startActivity(i);

            } else if (main_intent.getAction().equals(CLICK_ACTION_CHECKIN_BUTTON)) {
                Logger.d(TAG, "WidgetProvider CheckinButtonClicked");

                Intent i = new Intent(context.getApplicationContext(), widget_place_checkin_dialog.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.getApplicationContext().startActivity(i);

            } else if (main_intent.getAction().equals(CLICK_ACTION_REFRESH_BUTTON)) {
                Logger.d(TAG, "WidgetProvider RefreshButtonClicked");

                refresh_indicator = true;
                final int appWidgetId = main_intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, AppWidgetManager.INVALID_APPWIDGET_ID);
                final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

                RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.facedroid_widget_layout);
                updateViews.setViewVisibility(R.id.pb_refreshing, View.VISIBLE);
                updateViews.setViewVisibility(R.id.refreshBtn, View.INVISIBLE);
                appWidgetManager.updateAppWidget(appWidgetId, updateViews);

                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.facedroid_widget_layout);
                        updateViews.setViewVisibility(R.id.pb_refreshing, View.GONE);
                        updateViews.setViewVisibility(R.id.refreshBtn, View.VISIBLE);
                        appWidgetManager.updateAppWidget(appWidgetId, updateViews);
                    }
                }, 1000 * 10);

                Intent intent = new Intent(context, WidgetProvider.class);
                intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                intent.putExtra("updateType", "single");
                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                context.sendBroadcast(intent);

            } else if (main_intent.getAction().equals(REFRESH_END)) {
                Logger.d(TAG, "WidgetProvider RefreshEnd");

                if (refresh_indicator) {

                    int appWidgetId = main_intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, AppWidgetManager.INVALID_APPWIDGET_ID);
                    AppWidgetManager mgr = AppWidgetManager.getInstance(context);

                    RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.facedroid_widget_layout);
                    updateViews.setViewVisibility(R.id.pb_refreshing, View.GONE);
                    updateViews.setViewVisibility(R.id.refreshBtn, View.VISIBLE);
                    mgr.updateAppWidget(appWidgetId, updateViews);
                    refresh_indicator = false;
                }

                // if (WidgetService.isOrientationChanged) {
                // Log.e(TAG, "WidgetService.isOrientationChanged=true");
                // WidgetService.isOrientationChanged = false;
                // final int appWidgetId =
                // main_intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                // 0);
                // final AppWidgetManager appWidgetManager =
                // AppWidgetManager.getInstance(context);
                // updateAppWidgetSingle(context, appWidgetManager,
                // appWidgetId);
                // } else {
                // Log.e(TAG, "WidgetService.isOrientationChanged=false");
                // }
            }

        } else {
            Logger.d(TAG, "WidgetProvider (Account not logined)");

            Toast.makeText(context, "Please login to facedroid", Toast.LENGTH_SHORT).show();

            if (main_intent.getAction().equals(CLICK_ACTION_COMPLETE_WIDGET_CLICK)) {
                Logger.d(TAG, "WidgetProvider HeaderWidgetClicked");

                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                ComponentName component_name = new ComponentName("com.platinumapps.facedroid", "com.platinumapps.activities.Splash_Activity");
                intent.setComponent(component_name);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                if (intent != null) {
                    context.startActivity(intent);
                }

            } else if (main_intent.getAction().equals(CLICK_ACTION_WIDGET_CONTAINER_AREA)) {
                Logger.d(TAG, "WidgetProvider ContainerWidgetClicked");

                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                ComponentName component_name = new ComponentName("com.platinumapps.facedroid", "com.platinumapps.activities.Splash_Activity");
                intent.setComponent(component_name);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                if (intent != null) {
                    context.startActivity(intent);
                }
            }
        }

        String updateType = main_intent.getStringExtra("updateType");
        Logger.d(TAG, "updateType=" + updateType);
        if (updateType != null) {
            if (updateType.equals("single")) {
                final int appWidgetId = main_intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
                final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                updateAppWidgetSingle(context, appWidgetManager, appWidgetId);
            } else if (updateType.equals("multiple")) {
                final int[] appWidgetIds = (int[]) main_intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                updateAppWidgetMultiple(context, appWidgetManager, appWidgetIds);
            }
        }

        Logger.d(TAG, "WidgetProvider EXIT onReceive");
        //super.onReceive(context, main_intent);
    }
4

2 回答 2

1

当设备的方向改变时,会调用 Activity 的 onCreate 方法。因此,您必须保留数据,然后在 onCreate 方法中检索它。看看 http://developer.android.com/guide/topics/resources/runtime-changes.html

于 2013-02-20T11:43:47.343 回答
1

当你旋转手机时,活动被破坏并重新创建。所以变量被重新初始化。这就是为什么data does not display。你可以通过阻止活动 onRotation 的破坏来克服这个问题。要实现这一点,请将其放入您的清单中:

<activity android:name="FriendPickerActivity"
android:label="@string/app_name" 
android:configChanges="orientation"/>

我指的是android:configChanges="orientation"

或者你可以在你的活动中使用onSaveInstanceState ()onRestoreInstanceState ()

于 2013-02-20T12:02:46.000 回答