Android(安卓)飞行模式的设置(打开/关闭飞行模式,获取飞行状态状态)
16lz
2022-06-08
转自: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); } } }
更多相关文章
- Android(安卓)-- Wifi启动流程分析
- 用kotlin打印出漂亮的android日志(三)——基于责任链模式打印任
- 关闭Android(安卓)root权限
- Android(安卓)Recovery 模式
- Android四种Activity的加载模式
- Android(安卓)开机震动的调用位置以及打开关闭方法
- Android(安卓)4.3新特性——SElinux简介
- 设置屏幕显示模式ScreenOrientation.
- android五种布局模式