2013-04-25 2 views
0

Я отчасти новичок в программировании для Android. Я пытаюсь сделать виджет, который выключается и находится в режиме полета. У меня возникли проблемы с обновлением виджета после того, как я вручную изменил режим полета в настройках. Я предположил, что функция onUpdate будет вызвана, как только я вернусь на Homescreen, но я ошибаюсь. Как я могу это решить?Настройки изменения виджетов не обновляются после изменения вручную настроек

Ниже мой код: Main 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 код

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

     } 
    } 

Заранее спасибо

ответ

0

Я думаю, вы не должны проверить режим полета, но проверить Интернет.

//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(); 
} 

и огонь вашей задачей, только если сеть нормально .. Вы можете проверить режим полета, если нет сети и показать тост «пожалуйста, включите в режим полета».

Надеюсь, что это поможет :)

Смежные вопросы