[英]Widget change settings doesn't update after manually changing settings
I am kinda new to android programming. 我对android编程有点陌生。 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.
我以为一旦我回到主屏幕就会调用onUpdate函数,但是我错了。 How would I solve this?
我该如何解决?
Below is my code: Main Widget 下面是我的代码: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);
}
}
This is my IntentReceiver Code 这是我的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);
}
}
}
Thanks in advance 提前致谢
I guess you should not check Airplane mode but check Internet. 我想您不应该检查飞行模式,而要检查互联网。
//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();
}
and fire your task only if network is ok.. You could check Airplane mode, if there is no network and Show a toast "please turn of Airplane mode". 并仅在网络正常的情况下执行任务。.如果没有网络,您可以检查飞行模式,并举杯“请转飞飞行模式”。
Hope that helps :) 希望有帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.