How should I programmatically open the Flight Mode

2019-05-20 20:48发布

I am opening the Flight Mode setting page with below code.

startActivity(new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS));

It is working fine on the most of the devices. But on Samsung model = GT-19300 it throws an exception:

ActivityNot Found Exception.No Activity found to handle intent {android.Settings.ACTION_AIRPLANE_MODE_SETTINGS}

What is the Intent string to open Flight Mode settings on Samsung devices?

2条回答
可以哭但决不认输i
2楼-- · 2019-05-20 21:41

I've tried the @Rizwan Rasheed's answers, and the else block opened an activiy which was not the Airplane Mode Activity. I've done this in a Samsung J1.

In accordance with the documentation:

In some cases, a matching Activity may not exist, so ensure you safeguard against this.

As an option, in catch block I suggest you open the device settings or some alert dialog that opens the same activity on click event.

try {
    Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}
catch (ActivityNotFoundException e){
    // Open a dialog or device settings
}

To open device settings, do this:

startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
查看更多
Root(大扎)
3楼-- · 2019-05-20 21:48
if (android.os.Build.VERSION.SDK_INT < 17){
    try{
        Intent intentAirplaneMode = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
        intentAirplaneMode.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intentAirplaneMode);
    }
    catch (ActivityNotFoundException e){
        Log.e("exception", e + "");
    }
}
else{
    Intent intent1 = new Intent("android.settings.WIRELESS_SETTINGS");
    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent1);
}

try this code hope it will work.

查看更多
登录 后发表回答