Android利用代码开启关闭飞行模式
2017-04-27 17:28
423 查看
今天做项目时候,需要用到代码来开关Android的飞行模式,其实比较简单啦,哦,在4.2之前.
在Android中设置飞行状态是用BroadCast的,可以通过发送action为”Intent.ACTION_AIRPLANE_MODE_CHANGED”的广播来打开或状态飞行模式.
首先,修改飞行模式需要android.permission.WRITE_SETTINGS权限,请自行添加.对权限就是一个!
接着是一个布局文件 就两个按钮
最后是java实现,也比较简单易懂
这样最后的实现效果就是点击开启飞行模式就会开启,关闭就会关了,就不上图啦!
在Android中设置飞行状态是用BroadCast的,可以通过发送action为”Intent.ACTION_AIRPLANE_MODE_CHANGED”的广播来打开或状态飞行模式.
首先,修改飞行模式需要android.permission.WRITE_SETTINGS权限,请自行添加.对权限就是一个!
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.zgf.flymode"> <uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
接着是一个布局文件 就两个按钮
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.zgf.flymode.MainActivity"> <Button android:id="@+id/btn_start" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/start"/> <Button android:id="@+id/btn_close" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/close"/> </LinearLayout>
最后是java实现,也比较简单易懂
package com.example.zgf.flymode; import android.support.annotation.IdRes; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.provider.Settings; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; public class MainActivity extends AppCompatActivity { private Button btn_start; private Button btn_close; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_start=(Button)findViewById(R.id.btn_start); btn_close=(Button)findViewById(R.id.btn_close); final ContentResolver cr = getContentResolver(); btn_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 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); } }); btn_close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Settings.System.putString(cr,Settings.System.AIRPLANE_MODE_ON, "0"); Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); sendBroadcast(intent); } }); } }
这样最后的实现效果就是点击开启飞行模式就会开启,关闭就会关了,就不上图啦!
相关文章推荐
- Android 用代码开启和关闭飞行模式
- Android用代码实现开启关闭调试模式
- android利用Handler开启线程和关闭线程
- android监控程序开启与关闭的代码
- Android代码实现飞行模式的打开
- Android 飞行模式的设置(打开/关闭飞行模式,获取飞行状态状态)
- android 代码开启和关闭软键盘
- iOS设备中WiFi、蓝牙和飞行模式的开启与关闭
- Android 开启gps, 数据流量, 飞行模式.
- [转]iOS设备中WiFi、蓝牙和飞行模式的开启与关闭
- android闪关灯的开启和关闭方法代码实例
- 【Android】代码开启/关闭GPS
- 关于Android打开/关闭飞行模式的一些思考
- android怎样用代码静默开启或关闭GPS。
- Android 飞行模式的设置(打开/关闭飞行模式,获取飞行状态状态)
- iOS设备中WiFi、蓝牙和飞行模式的开启与关闭
- 飞行模式开启时关闭收音机
- android中如何通过代码来开启和关闭移动网络
- 利用反射Android开发之开启关闭数据连接实现
- 无废话Android之activity的生命周期、activity的启动模式、activity横竖屏切换的生命周期、开启新的activity获取他的返回值、利用广播实现ip拨号、短信接收广播、短信监听器(6)