Sto cercando di impostare la modalità aereo in un Nexus 4 con Android 4.2.2. So che non è possibile poiché AIRPLANE_MODE_ON è stato spostato su Global system settings ed è solo un'opzione di lettura.Modalità aereo in Jelly Bean
C'è qualche altro modo per fare qualcosa di simile, voglio dire disabilitare la radio? Posso disabilitare la connessione Bluetooth, Wi-Fi e Internet, ma la rete di chiamate telefoniche è ancora attiva.
Potrebbe essere possibile creare una libreria con NDK per disabilitare completamente la rete?
EDIT: ho provato con java.lang.reflect
rendendo questo metodo:
@SuppressLint("NewApi")
@SuppressWarnings("rawtypes")
private boolean putStringAirplaneMode() throws ClassNotFoundException,
NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
ContentResolver resolver = context.getContentResolver();
String name = Settings.Global.AIRPLANE_MODE_ON;
String value = (isEnabled() ? "1" : "0");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Log.v("AirplaneBatterySaver",
"Using reflection to change airplane mode");
// For JELLY_BEAN_MR1 use reflection. TODO test if works reflection
Class SystemProperties = android.provider.Settings.Global.class;
Class[] parameterTypes = new Class[3];
parameterTypes[0] = ContentResolver.class;
parameterTypes[1] = String.class;
parameterTypes[2] = String.class;
@SuppressWarnings("unchecked")
Method method = SystemProperties.getMethod("putString",
parameterTypes);
method.setAccessible(true);
return (Boolean) method.invoke(new Object(), resolver, name, value);
// return Settings.Global.putString(resolver, name, value);
} else {
Log.v("AirplaneBatterySaver",
"Using Settings.System to change airplane mode");
return Settings.System.putString(resolver, name, value);
}
}
Come potete vedere ho sostituire il metodo Settings.System.putString(resolver, name, value);
sopra JELLY_BEAN_MR1
ma, naturalmente, sto ottenendo un SecurityException
perché la mia app non è un'app di sistema. Questa è la traccia:
Caused by: java.lang.SecurityException: Permission denial: writing to secure
settings requires android.permission.WRITE_SECURE_SETTINGS
at android.os.Parcel.readException(Parcel.java:1425)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:574)
at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:777)
at android.provider.Settings$Global.putStringForUser(Settings.java:5421)
at android.provider.Settings$Global.putString(Settings.java:5411)
Se uso <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
ottengo l'errore Permission is only granted to system apps
. Stavo controllando i sorgenti SDK (nei file dalla traccia) per cercare di trovare un modo per impostare la modalità aereo senza questo permesso, ma non ho avuto alcun successo.
La risposta a questa domanda è già disponibile in questo sito! Prova a cercare "4.2 modalità aereo". Fondamentalmente, non è più possibile modificare questa impostazione dalla 4.2, a meno che l'applicazione non abbia root (ci sono risposte che spiegano come fare anche questo). – shadrx
Grazie per la risposta. In realtà non sto cercando come modificare questa impostazione (modalità aereo), comunque cerco più duro in StackOverflow. Sto cercando come fare qualcosa di simile in un modo diverso (se è possibile). Per la mia app utilizzare le autorizzazioni di root non è un'opzione. – Freefri