2013-04-25 198 views
0

我對Android編程有點新鮮。我正在嘗試製作一個關閉並處于飛行模式的小部件。在手動更改設置中的飛行模式之後,我在更新小部件時遇到問題。我認爲一旦我回到主屏幕就會調用onUpdate函數,但我錯了。我將如何解決這個問題?手動更改設置後,Widget更改設置不會更新

下面是我的代碼:主窗口部件

import android.app.PendingIntent; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.RemoteViews; 
import android.provider.Settings; 
import android.widget.Toast; 
import android.util.Log; 

public class AirplaneModeWidget extends AppWidgetProvider 
{ 
    private static boolean state; 
    private static String TAG = "APMode_WIDGET"; 
    public void onCreate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
    { 
     Log.i(TAG,"In onCreate"); 
     state = isAirplaneMode(context); 
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.airplane_mode); 
     remoteViews.setOnClickPendingIntent(R.id.ibtn_airplane_mode, buildButtonPendingIntent(context)); 
     updateUI(remoteViews); 
     pushWidgetUpdate(context, remoteViews); 
    } 
    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
    { 
     Log.i(TAG,"In onUpdate"); 
     state = isAirplaneMode(context); 
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.airplane_mode); 
     remoteViews.setOnClickPendingIntent(R.id.ibtn_airplane_mode, buildButtonPendingIntent(context)); 
     updateUI(remoteViews); 
     pushWidgetUpdate(context, remoteViews); 

    } 

public static PendingIntent buildButtonPendingIntent(Context context) 
{ 

     Intent intent = new Intent(); 
     intent.setAction("com.ps.transparenttogglewidget.intent.action.CHANGE_STATUS"); 
     return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    } 

    public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) 
    { 
     ComponentName myWidget = new ComponentName(context, AirplaneModeWidget.class); 
     AppWidgetManager manager = AppWidgetManager.getInstance(context); 
     manager.updateAppWidget(myWidget, remoteViews);  
    } 

    public static void updateUI(RemoteViews rview) 
    { 
     Log.i(TAG,"in updateUI"); 
     Log.i(TAG,"State is" + state); 
     if(state) 
     { 
      rview.setImageViewResource(R.id.ibtn_airplane_mode,R.drawable.ic_hw_airplane_on); 
     } 
     else 
     { 
      rview.setImageViewResource(R.id.ibtn_airplane_mode,R.drawable.ic_hw_airplane_off); 
     } 
    } 
    public static boolean isAirplaneMode(Context context) 
    { 
     Log.i(TAG,"in isAirplaneMode"); 
     return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1; 
    } 
    public static void setAirplaneMode(Context context) 
    { 
     Log.i(TAG,"in setAirplaneMode"); 
     state = isAirplaneMode(context); 
     Toast.makeText(context,"state :"+state,Toast.LENGTH_SHORT).show(); 
     Log.i(TAG,"state is "+state); 
     Settings.System.putInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, state ? 0 : 1); 
     Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
     intent.putExtra("state",!state); 
     context.sendBroadcast(intent); 
     state = isAirplaneMode(context); 
    } 
} 

這是我的IntentReceiver代碼提前

回答

0

我想你不應該檢查飛行模式,但檢查互聯網

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import android.widget.RemoteViews; 


    public class AirplaneModeIntentReceiver extends BroadcastReceiver 
    { 
     private static String TAG = "APMode_WIDGET"; 
     public void onReceive(Context context, Intent intent) 
     { 
      Log.i(TAG,"in onReceive"); 
      if(intent.getAction().equals("com.ps.transparenttogglewidget.intent.action.CHANGE_STATUS")) 
      { 
       AirplaneModeWidget.setAirplaneMode(context); 
       RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.airplane_mode); 
       remoteViews.setOnClickPendingIntent(R.id.ibtn_airplane_mode, AirplaneModeWidget.buildButtonPendingIntent(context)); 
       AirplaneModeWidget.updateUI(remoteViews); 
       AirplaneModeWidget.pushWidgetUpdate(context, remoteViews); 
      } 

     } 
    } 

感謝。

//check internet connection 
public static boolean isNetworkAvailable(Context context) { 
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
} 

和解僱你的任務只有當網絡是確定.. 你可以檢查飛行模式,如果沒有網絡和顯示舉杯「請打開飛行模式的」。

希望能幫到:)