转自:http://www.pocketdigi.com/20110714/391.html

在Android中设置飞行状态是用BroadCast的,可以通过发送action为”Intent.ACTION_AIRPLANE_MODE_CHANGED”的广播来打开或关闭飞行模式状态.
首先,修改飞行模式需要android.permission.WRITE_SETTINGS权限,请自行添加.
下面是完整代码:

package com.hello;import android.app.Activity;import android.content.ContentResolver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;import android.widget.Toast;import android.provider.Settings;//虽然只用到Settings.System类,但还是不建议直接导入该类,因为会跟java.lang.System同名冲突//当然也可以不导,直接用android.provider.Settings.Systempublic class HelloWorldActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        ContentResolver cr = getContentResolver();        if(Settings.System.getString(cr,Settings.System.AIRPLANE_MODE_ON).equals("0")){        //获取当前飞行模式状态,返回的是String值0,或1.0为关闭飞行,1为开启飞行        //如果关闭飞行,则打开飞行        Settings.System.putString(cr,Settings.System.AIRPLANE_MODE_ON, "1");        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);        sendBroadcast(intent);        }else{        //否则关闭飞行        Settings.System.putString(cr,Settings.System.AIRPLANE_MODE_ON, "0");        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);        sendBroadcast(intent);        }     } }

更多相关文章

  1. Android中关于startActivityForResult()在activity不同启动模式
  2. android 如何在状态栏上增加一个icon
  3. Android Ap 开发 设计模式第二篇:适配器模式

随机推荐

  1. Android(安卓)Camera出现无法连接相机问
  2. android 查看apk签名信息
  3. android 源码环境下模拟器
  4. android blog 关注
  5. Android电视关闭的动画效果
  6. Android获取标题栏高度
  7. android读取data/data/包名/file路径下的
  8. Android中文api:android.accessibilityser
  9. Android(安卓)Activity组件的启动过程
  10. android http下载