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

あなたがあなたの電話を回転させるとき、活動は破壊されて再作成されました。したがって、変数は再初期化されます。それが理由data does not displayです。uは、onRotationのアクティビティの破棄をブロックすることでこれを克服できます。これを達成するには、これをマニフェストに入れます。

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

私は指していますandroid:configChanges="orientation"

またはuはあなたの活動でonSaveInstanceState ()使用することができますonRestoreInstanceState ()

于 2013-02-20T12:02:46.000 に答える
1

デバイスの向きが変更されると、Activity の onCreate メソッドが呼び出されます。そのため、データを永続化してから onCreate メソッドで取得する必要があります。http://developer.android.com/guide/topics/resources/runtime-changes.htmlを見てください。

于 2013-02-20T11:43:47.343 に答える