2017-03-31 54 views
0

我想刷新android手機網絡android,但得到錯誤。我想讓它工作。如何打開/關閉蜂窩網絡Android?

我的代碼:

Settings.System.putInt(getContentResolver(), 
       Settings.Global.AIRPLANE_MODE_ON, mode ? 1 : 0);// Turning ON/OFF Airplane mode. 

     Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);// creating intent and Specifying action for AIRPLANE mode. 
     intent.putExtra("state", !mode);// indicate the "state" of airplane mode is changed to ON/OFF 
     sendBroadcast(intent);// Broadcasting and Intent 

我的清單文件有權限。

<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 

錯誤:

FATAL EXCEPTION: main 
                     Process: com.zt.refreshnetwork, PID: 20221 
                     java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE from pid=20221, uid=10237 
                      at android.os.Parcel.readException(Parcel.java:1684) 
                      at android.os.Parcel.readException(Parcel.java:1637) 
                      at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:3537) 
                      at android.app.ContextImpl.sendBroadcast(ContextImpl.java:881) 
                      at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:421) 
                      at com.zt.refreshnetwork.MainActivity.modifyAirplanemode(MainActivity.java:140) 
                      at com.zt.refreshnetwork.MainActivity.airPlanemodeON(MainActivity.java:117) 
                      at com.zt.refreshnetwork.MainActivity$1.onClick(MainActivity.java:46) 
                      at android.view.View.performClick(View.java:5637) 
                      at android.view.View$PerformClick.run(View.java:22429) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
+0

'造成的:java.lang.SecurityException異常:無論用戶10237也不是當前進程android.permission.MODIFY_PHONE_STATE>'似乎是一個非常強烈的暗示。 – Michael

+0

查看Logcat,您需要'MODIFY_PHONE_STATE'權限,但這僅適用於系統應用程序或根源設備([documentation](https://developer.android.com/reference/android/Manifest.permission.html #MODIFY_PHONE_STATE))。你究竟想要做什麼? –

+0

@Michael不允許從pid = 20221,uid = 10237 –

回答

0

你提到你正在試圖修改飛行模式狀態。從Android 4.2開始,它已被移除到第三方應用程序。 From the release notes:

Several existing settings were relocated here from either Settings.System or Settings.Secure . If your app is currently making changes to settings previously defined in Settings.System (such as AIRPLANE_MODE_ON), then you should expect that doing so will no longer work on a device running Android 4.2 or higher if those settings were moved to Settings.Global .

You can continue to read settings that are in Settings.Global , but because the settings are no longer considered safe for apps to change, attempting to do so will fail silently and the system will write a warning to the system log when running your app on Android 4.2 or higher.

機會是你需要依靠反射和周圍的AOSP源挖,如果你絕對要做到這一點,但谷歌通常做出良好的安全原因,這些種種變化。任何規避這些措施的嘗試都可能導致您的應用從Play商店中被移除,或者如果底層方法發生變化,可能會導致您的應用根本無法在將來的Android版本中使用。

同樣,在您的問題的原始版本中,您的應用請求MODIFY_PHONE_STATE權限。此外,這不適用於第三方應用程序,只有系統應用程序,這將需要此應用程序在根設備上運行。 From the documentation (my bold):

Allows modification of the telephony state - power on, mmi, etc. Does not include placing calls. Not for use by third-party applications.

+0

請檢查這個程序。它在牛軋糖工作。 https://play.google.com/store/apps/details?id=com.atejapps.networksignalrefresher&hl=en –

+0

您需要聯繫應用程序的開發人員以確定其使用的方法。 –