2013-02-20 74 views
1

我開發了一個應用程序,它具有小部件,這些工作正常,但是我改變了設備的方向,它顯示得很好。但有些時候,當我改變方向數據不顯示。任何人都可以幫助我,爲什麼這個問題會發生一些事情。 這裏是代碼當設備方向改變時widget會丟失數據

@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); 
    } 

回答

1

當你旋轉你的手機的活動銷燬和重新創建。所以變量被重新初始化。這就是爲什麼data does not display。你可以通過阻止旋轉對活動的破壞來克服這一點。實現這一把這個在你的清單:

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

我正在指着android:configChanges="orientation"

或U可以在您的活動

+0

我對開發的小部件有嚴格的問題。我將在代碼中添加這個小技巧。讓我們希望它有效! – durbnpoisn 2014-04-11 19:08:04

1

當設備的方向改變時,活動的onCreate方法被調用的代碼。所以你將不得不堅持數據,然後在onCreate方法中檢索它。看看 http://developer.android.com/guide/topics/resources/runtime-changes.html

+0

你的回答是好,使用onSaveInstanceState()onRestoreInstanceState()和相關礦井的話題,但我已經處理了這個。這個問題只發生在一段時間,並不是每一次。這個問題與超類調用有關。 – 2013-02-21 06:46:13