本文原做者 长鸣鸟 ,未经赞成,转载不带名的严重鄙视。
做为系统开发者,咱们每每有这样那样修改系统属性的需求,例如修改国家码,如persist.sys.countrycode之类。但咱们不能把每个应用都给予系统权限,这样指不定哪天会出大事,并且客户也不一样意。
因此咱们就须要一种剑走偏锋,曲线救国之法:
有修改属性需求的应用发送广播,有权限的应用接收广播,修改属性。
发送方:android
private static final String BACKGROUNDDATA_ON = "#backgtotrue#"; private static final String BACKGROUNDDATA_OFF = "#backtofalse#"; Intent intent = new Intent("android.mine.SECRET_CODE"); if(enableExp){ intent.putExtra("secretcode", BACKGROUNDDATA_ON); } else{ intent.putExtra("secretcode", BACKGROUNDDATA_OFF); } this.sendBroadcast(intent);
接收方:this
private static final String BACKGROUNDDATA_ON = "#backgtotrue#"; private static final String BACKGROUNDDATA_OFF = "#backtofalse#"; String action = intent.getAction(); String secretcode = intent.getStringExtra("secretcode"); if ("android.mine.SECRET_CODE".equals(action)) { if (BACKGROUNDDATA_ON.equals(secretcode)) { Log.d(TAG, "persist.backgrounddata.enable:true"); SystemProperties.set("persist.backgrounddata.enable", "true"); } else if (BACKGROUNDDATA_OFF.equals(secretcode)) { Log.d(TAG, "persist.backgrounddata.enable:false"); SystemProperties.set("persist.backgrounddata.enable", "false"); } }
但这样可能不够严谨,毕竟谁均可以发送广播,谁也能够接收广播。咱们想要的是1对1,就要在在代码里声明一对一。
本文原做者 长鸣鸟 ,未经赞成,转载不带名的严重鄙视。
方案1:指定接收者
发送方:
AdroidManifest.xml:code
+<permission android:name = "com.android.permission.RECV_ONLY"/>
而后发送广播的时候附带权限:xml
sendBroadcast("android.mine.SECRET_CODE", "com.android.permission.RECV_ONLY");
接收方:
AndroidManifest.xml:开发
+<uses-permission android:name="com.android.permission.RECV_ONLY"></uses-permission>
方案2:指定发送者
接收方:
AdroidManifest.xml:get
+<permission android:name="com.android.SEND_ONLY"/>
而后修改接收器:it
<receiver android:name=".mReceiver" +android:permission="com.android.permission.SEND_ONLY"> <intent-filter> <action android:name="com.mine.SECRET_CODE" /> </intent-filter> </receiver>
发送方:
AdroidManifest.xml:io
<uses-permission android:name="com.android.permission.SEND_ONLY"></uses-permission>
本文原做者 长鸣鸟 ,未经赞成,转载不带名的严重鄙视。
Enjoy it!ast