Android 9.0 开启飞行模式

news/2024/11/8 0:29:41/

这篇文章针对的是系统权限的app,因为

Intent.ACTION_AIRPLANE_MODE_CHANGED 在AndroidManifest.xml文件中属于<protected-broadcat/>,需要权限需要高权限才能操作,
//Intent.class
public class Intent implements Parcelable, Cloneable {
...public static final String ACTION_AIRPLANE_MODE_CHANGED = "android.intent.action.AIRPLANE_MODE";
...
}//  framework/base/core/res/AndroidManifest.xml
<protected-broadcast android:name="android.intent.action.AIRPLANE_MODE" />

 应为Android 发送广播最终要走 ActivityManagerService 的broadcastIntentLocked 方法,我贴一段发送广播的代码,在里面有一段代码是判断当前 app的 uid权限的,如果权限不够则直接跑出异常

 @GuardedBy("this")final int broadcastIntentLocked(ProcessRecord callerApp,String callerPackage, Intent intent, String resolvedType,IIntentReceiver resultTo, int resultCode, String resultData,Bundle resultExtras, String[] requiredPermissions, int appOp, Bundle bOptions,boolean ordered, boolean sticky, int callingPid, int callingUid, int userId) {...final boolean isCallerSystem;switch (UserHandle.getAppId(callingUid)) {case ROOT_UID:case SYSTEM_UID:case PHONE_UID:case BLUETOOTH_UID:case NFC_UID:case SE_UID:isCallerSystem = true;break;default:isCallerSystem = (callerApp != null) && callerApp.persistent;break;}// First line security check before anything else: stop non-system apps from// sending protected broadcasts.if (!isCallerSystem) {if (isProtectedBroadcast) {String msg = "Permission Denial: not allowed to send broadcast "+ action + " from pid="+ callingPid + ", uid=" + callingUid;Slog.w(TAG, msg);throw new SecurityException(msg);} else if (AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(action)|| AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {// Special case for compatibility: we don't want apps to send this,// but historically it has not been protected and apps may be using it// to poke their own app widget.  So, instead of making it protected,// just limit it to the caller.if (callerPackage == null) {String msg = "Permission Denial: not allowed to send broadcast "+ action + " from unknown caller.";Slog.w(TAG, msg);throw new SecurityException(msg);} else if (intent.getComponent() != null) {// They are good enough to send to an explicit component...  verify// it is being sent to the calling app.if (!intent.getComponent().getPackageName().equals(callerPackage)) {String msg = "Permission Denial: not allowed to send broadcast "+ action + " to "+ intent.getComponent().getPackageName() + " from "+ callerPackage;Slog.w(TAG, msg);throw new SecurityException(msg);}} else {// Limit broadcast to their own package.intent.setPackage(callerPackage);}}}...  return ActivityManager.BROADCAST_SUCCESS;}

 /*** 飞行模式开关* @param setAirPlane*/public static void toggleAirplaneMode(Context context,boolean setAirPlane) {Log.d(TAG, "toggleAirplaneMode: "+setAirPlane);String value = setAirPlane?"1":"0";Log.d(TAG, "toggleAirplaneMode: "+value);Settings.Global.putString( context.getContentResolver(),Settings.Global.AIRPLANE_MODE_ON,value );Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);context.sendBroadcast(intent);}


http://www.ppmy.cn/news/644139.html

相关文章

安卓打开飞行模式

一、设置飞行模式代码 ContentResolver cr getContentResolver(); if(Settings.System.getString(cr,Settings.System.AIRPLANE_MODE_ON).equals("0")){//获取当前飞行模式状态,返回的是String值0,或1.0为关闭飞行,1为开启飞行//如果关闭飞行,则打开飞行Settings.S…

我在飞机上,飞机在哪里?浅谈飞行模式与GPS定位

&#x1f4e2; 声明&#xff1a; &#x1f96d; 作者主页&#xff1a;【MangoPapa的CSDN主页】。⚠️ 本文首发于CSDN&#xff0c;转载或引用请注明出处【点击查看原文】。⚠️ 本文为非盈利性质&#xff0c;目的为 个人学习记录 及 知识分享。因个人能力受限&#xff0c;存在…

Android 设置飞行模式,判断是否是飞行模式

飞行模式的好处在于&#xff1a; 1.可以让你拥有一个安静的休息时间 2.减少对身体的辐射 3.可以节省电量 4.不会关闭闹钟 所以设置飞行模式的功能&#xff0c;是很有用处的。 现分享如下&#xff1a; 代码中设置飞行模式所需权限&#xff1a; <uses-permission android:name…

2 线程基础知识复习

1、并发相关Java包 涉及到的包内容 java.util.concurrent java.util.concurrent.atomic java.util.concurrent.locks 2、并发始祖 3、start线程解读 初始程序 public static void main(String[] args) {Thread t1 new Thread(() ->{},"t1");t1.start();}//…

怎么看台式计算机是几位的,怎么查看自己电脑型号

在电脑上如何查看自己电脑的型号? 在电脑上查看自己电脑型号其实就是查看电脑硬件的型号&#xff0c;查看的方法很多。下面是小编为大家介绍查看自己电脑型号的方法,欢迎大家阅读。 查看自己电脑型号 第一种方法&#xff1a;用软件查看电脑型号 如&#xff1a;用CPU-Z查看电脑…

查看笔记本电脑型号等数据

命令方式 cmd 运行窗口输入命令&#xff1a;dxdiag 弹出软硬件信息&#xff1a;

计算机主机型号查询,如何查询电脑硬件的型号

对于我们平时常用的电脑&#xff0c;不知道大家会不会查询你们电脑硬件的型号呢&#xff0c;下面应届毕业生培训网小编就与大家分享几个经测试还不错的方法&#xff0c;希望对大家有所帮助。 任务&#xff1a;在电脑上如何查看自己电脑的型号?在电脑上查看自己电脑型号其实就是…

Windows 查看电脑型号

1、winR打开敏玲提示符窗口&#xff0c;输入 dxdiag&#xff0c;如图 2、确定后显示界面&#xff0c;即可查看电脑型号以及相关属性