プログラムでモバイル データ接続を無効にする必要がある
私はこれに従った
private void setMobileDataEnabled(Context context, boolean enabled) {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}
しかし、「java.lang.NullPointerException:Println にはメッセージが必要です」というエラーが表示されました
ログ
09-05 18:20:29.081: E/AndroidRuntime(736): FATAL EXCEPTION: Timer-0
09-05 18:20:29.081: E/AndroidRuntime(736): java.lang.NullPointerException: println needs a message
09-05 18:20:29.081: E/AndroidRuntime(736): at android.util.Log.println_native(Native Method)
09-05 18:20:29.081: E/AndroidRuntime(736): at android.util.Log.d(Log.java:138)
09-05 18:20:29.081: E/AndroidRuntime(736): at com.anand.batterysaver.BatterySaverActivity.checkScreenStatus(BatterySaverActivity.java:69)
09-05 18:20:29.081: E/AndroidRuntime(736): at com.anand.batterysaver.BatterySaverActivity$1.run(BatterySaverActivity.java:43)
09-05 18:20:29.081: E/AndroidRuntime(736): at java.util.Timer$TimerImpl.run(Timer.java:284)
checkScreenStatus 関数
public void checkScreenStatus(){
PowerManager powermanager;
GetProcessList list = new GetProcessList();
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
powermanager = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
if (powermanager.isScreenOn() == false)
{
Log.d("PowerStatus","Lock All Settings");
boolean gprsstatus = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED;
Log.d("Locked gprsStatus",String.valueOf(gprsstatus));
if(gprsstatus == true){
try{
list.setDataEnabled(getApplicationContext(), false);
}catch (Exception e) {Log.d("Exception",e.getMessage()); }
}
}else{
Log.d("PowerStatus","Resume All");
gprsstatus = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED;
Log.d("Locked gprsStatus",String.valueOf(gprsstatus));
if(gprsstatus == true){
TelephonyManager gprs = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
if(gprs.getDataState() == TelephonyManager.DATA_CONNECTED){
Log.d("Gprs","on");
}
}
}
}