我多次发现此问题,但所有解决方案似乎均无效。
清单中有以下内容:
<receiver android:name=".noactivity.Airplane">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE_CHANGED"/>
</intent-filter>
</receiver>
以及以下课程:
public class Airplane extends BroadcastReceiver {
public Airplane(){
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AirplaneMode", "Service state changed");
Toast.makeText(context,"Airplane mode changed",Toast.LENGTH_LONG).show();
}}
但是,如果我更改飞行模式,则什么也不会发生。可以肯定的是,我还添加了:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(new Airplane(), new IntentFilter("android.intent.action.AIRPLANE_MODE_CHANGED"));
}}
什么也不起作用。 你们中有人知道我在做什么吗?
答案 0 :(得分:0)
我成功使用了此选项,而未在清单中添加接收者。 在您的活动中添加以下内容:
@Override
protected void onResume() {
//register receiver for air plane mode states
IntentFilter airPlaneModeFilter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(airPlaneModeReceiver, airPlaneModeFilter);
super.onResume();
}
private BroadcastReceiver airPlaneModeReceiver = new BroadcastReceiver() {@Override
public void onReceive(Context context, Intent intent) {
boolean airPlaneModeEnabled = intent.getBooleanExtra("state", false);
if (airPlaneModeEnabled) {
} else {
}
}
};
@Override
protected void onPause() {
if (airPlaneModeReceiver != null) unregisterReceiver(airPlaneModeReceiver);
super.onPause();
}