Android切换飞行模式(SDK4.2之前或之后都可用)

时间:2022-05-26 23:59:43

SDK4.2之后对Setting做了修改,具体修改的地方没有仔细研究。

下面根据 这一情况给出SDK4.2之后的AirPlaneMode切换的源码。

这个是在国外的一个博客上面看到的,亲测 代码可行。

package com.margi.airplanemode;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ToggleButton;


public class MainActivity extends ActionBarActivity {

private ToggleButton tb1, tb2;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}


public void init() {
tb1 = (ToggleButton) findViewById(R.id.tb_ariplanemode);
tb2 = (ToggleButton) findViewById(R.id.tb_wifimode);
boolean enable = isAirPlaneOn(this);
//界面加载的时候根据情况给出初始状态
if (enable) {
tb1.setChecked(true);
}
tb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean state = isAirPlaneOn(MainActivity.this);
if(state)
{
setAirPlaneMode(MainActivity.this,state,0);
}else
setAirPlaneMode(MainActivity.this,!state,1);

}
});



}


public void setAirPlaneMode(Context context, boolean enable,int value) {
/* Settings.System.putInt(context.getContentResolver(), "State", enable ? 1 : 0);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("stat", enable);
context.sendBroadcast(intent);*/
//注释部分为 4.2版本之前的做法,现在已经失效。

//对版本做出判断
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
//if less than verson 4.2
Settings.System.putInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON,value);
} else {
Settings.Global.putInt(
getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON,value);
}
// broadcast an intent to inform
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !enable);
sendBroadcast(intent);

}

public boolean isAirPlaneOn(Context context) {
/* int isEnable = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
return (isEnable==1)?true:false;*/

//注释部分,是4.2版本之前的做法,现在已经失效。

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
//android version lower than 4.2
return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0;

} else {
return Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}