我对android编程有点新意。我正在尝试制作一个关闭和飞行模式的小部件。手动更改设置中的飞行模式后,我无法更新小部件。我假设一旦我回到主屏幕就会调用onUpdate函数,但我错了。我该如何解决这个问题?
以下是我的代码:主窗口小部件
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 :(得分: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();
}
仅在网络正常时启动您的任务。 您可以检查飞行模式,如果没有网络并显示祝酒词“请转动飞行模式”。
希望有所帮助:)