Note that there are some explanatory texts on larger screens.

plurals
  1. POWidget change settings doesn't update after manually changing settings
    text
    copied!<p>I am kinda new to android programming. I am trying to make a widget that turns off and on the airplane mode. I am having trouble updating the widget after I manually change the Airplane Mode in the settings. I assumed that the onUpdate function would be invoked once I go back to the Homescreen, but I am wrong. How would I solve this?</p> <p>Below is my code: Main Widget </p> <pre><code>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); } } </code></pre> <p>This is my IntentReceiver Code</p> <pre><code>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); } } } </code></pre> <p>Thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload