不多说,上图,见代码。


package lab.sodino.airplane;import java.text.SimpleDateFormat;import java.util.Calendar;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.provider.Settings;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;/** * @author Sodino Email:sodinoopen@hotmail.com * */public class AirPlaneAct extends Activity {private TextView txtInfo;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);txtInfo = (TextView) findViewById(R.id.txtInfo);Button btnIsAirPlane = (Button) findViewById(R.id.btnIsAirPlane);btnIsAirPlane.setOnClickListener(new Button.OnClickListener() {public void onClick(View view) {isAirplaneModeOn();}});final Button btnSetAirPlane = (Button) findViewById(R.id.btnSetAirPlane);btnSetAirPlane.setOnClickListener(new Button.OnClickListener() {public void onClick(View view) {setAirplaneMode(true);}});final Button btnCancelAirPlane = (Button) findViewById(R.id.btnCancelAirPlane);btnCancelAirPlane.setOnClickListener(new Button.OnClickListener() {public void onClick(View view) {setAirplaneMode(false);}});IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");BroadcastReceiver receiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {Log.d("ANDROID_INFO", "Service state changed");Bundle bundle = intent.getExtras();if (bundle != null) {txtInfo.append(formateToLogTime(System.currentTimeMillis()) + " Service state changed action="+ intent.getAction() + "/n");/** * Set<String> set = bundle.keySet();<br/> * key=manual value=0 <br/> * key=cdmaRoamingIndicator value=-1 Integer<br/> * key=operator-numeric value=0<br/> * key=cssIndicator value=0 Boolean<br/> * key=operator-alpha-long value=0<br/> * key=networkId value=-1<br/> * key=state value=0 Integer<br/> * key=emergencyOnly value=0 Boolean<br/> * key=systemId value=-1 Integer<br/> * key=roaming value=0 Boolean<br/> * key=operator-alpha-short value=0<br/> * key=cdmaDefaultRoamingIndicator value=-1 Integer<br/> * key=radioTechnology value=2 Integer<br/> */int state = bundle.getInt("state");Log.d("ANDROID_INFO", "state = " + state);txtInfo.append(formateToLogTime(System.currentTimeMillis()) + " state = " + state);switch (state) {case 0x00:Log.d("ANDROID_INFO", "Connect the net successfully.");txtInfo.append(" Connect the net successfully.");btnSetAirPlane.setEnabled(true);btnCancelAirPlane.setEnabled(false);break;case 0x01:Log.d("ANDROID_INFO", "Try to connect the net.");txtInfo.append(" Try to connect the net.");btnSetAirPlane.setEnabled(false);btnCancelAirPlane.setEnabled(true);break;case 0x03:Log.d("ANDROID_INFO", "Set AirPlaneMode Successful.");txtInfo.append(" Set AirPlaneMode Successful.");btnSetAirPlane.setEnabled(false);btnCancelAirPlane.setEnabled(true);break;}txtInfo.append("/n");} else {Log.d("ANDROID_INFO", "bundle is null");}}};registerReceiver(receiver, intentFilter);}private void isAirplaneModeOn() {// 返回值是1时表示处于飞行模式int modeIdx = Settings.System.getInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);boolean isEnabled = (modeIdx == 1);txtInfo.append(formateToLogTime(System.currentTimeMillis()) + " AirPlaneMode " + isEnabled + "/n");}private void setAirplaneMode(boolean setAirPlane) {Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, setAirPlane ? 1 : 0);// 广播飞行模式信号的改变,让相应的程序可以处理。// 不发送广播时,在非飞行模式下,Android 2.2.1上测试关闭了Wifi,不关闭正常的通话网络(如GMS/GPRS等)。// 不发送广播时,在飞行模式下,Android 2.2.1上测试无法关闭飞行模式。Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);// intent.putExtra("Sponsor", "Sodino");// 2.3及以后,需设置此状态,否则会一直处于与运营商断连的情况intent.putExtra("state", setAirplane);sendBroadcast(intent);Toast toast = Toast.makeText(this, "飞行模式启动与关闭需要一定的时间,请耐心等待", Toast.LENGTH_LONG);toast.show();}/** * 将长整型时间数字转为字符串。 *  * @return 返回格式为: 22:15:09的时间 */public static String formateToLogTime(long time) {Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(time);SimpleDateFormat simpleDF = new SimpleDateFormat("HH:mm:ss");String logTime = simpleDF.format(calendar.getTime());return logTime;}}

本文内容归CSDN博客博主Sodino所有

转载请注明出处:http://blog.csdn.net/sodino/archive/2011/01/13/6134196.aspx

补充下,得加个权限:

<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>

更多相关文章

  1. 【Android】抽屉控件 SlidingDrawer 的简单示例
  2. Android(安卓)开发之webview页面返回到最顶关闭
  3. [Android] 监听系统网络连接打开或者关闭的消息
  4. Android(安卓)使用广播(BroadcastReceiver)传递数据
  5. Android(安卓)监听网络变化
  6. [Android设计模式]Android退出应用程序终极方法
  7. android的Home键的监听封装工具类(一)
  8. unity 关闭弹出android 提示框
  9. Android的设计模式

随机推荐

  1. ListView如何去掉无所谓的阴影
  2. 使用Visual Studio 2015开发Android(安卓
  3. [GoogleMap/GPS] 申请Android(安卓)API K
  4. Android(安卓)back键ANR,与onPause,onDesto
  5. Android(安卓)累计未读通知的数量
  6. Android打开系统设置界面
  7. Android(安卓)中使用Aspectj进行切面编程
  8. Ubuntu下编译android的linux内核
  9. Android(安卓)Service---在清单中声明一
  10. Android(安卓)Ap 开发 设计模式第二篇:适