本文原做者 長鳴鳥 ,未經贊成,轉載不帶名的嚴重鄙視。
做爲系統開發者,咱們每每有這樣那樣修改系統屬性的需求,例如修改國家碼,如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