在有人告诉我购买应用程序之前快速回复故事.=)
我刚拿到EVO,它可以很快地咀嚼电池.我下载了JuiceDefender来管理我的移动数据连接.这看起来相当不错.但是,设置非常有限(即使在付费版本上).
截至目前,我正在尝试开发更加可定制的节电应用程序.我想要做的主要事情是能够随意启用/禁用移动数据连接.
问题是我找不到任何代码片段或文章如何做到这一点.我发现的唯一的事情是以下.我不知道这是多么准确,但这就是我可以拼凑浏览developer.android.com
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
cm.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "android.net.conn.CONNECTIVITY_CHANGE");
State state = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
textView.setText(state.name());
Run Code Online (Sandbox Code Playgroud)
如果有人能指出任何有帮助的事情,那将是非常感激的.
似乎Sprint上的HTC Evo不使用APN设置.我通过下载APNDroid并观察它无法正常测试.然后,我制作了一个快速应用程序,将所有APN条目转储到屏幕上.这产生了一个结果,它是为了mms.
查看JuiceDefender运行时的电话信息,我发现GSRP网络正在打开和关闭.这让我相信通过代码可以做到这一点,即使我发现询问同一问题的每一页都表示无法完成.踢球者是他们所说的像APNDroid一样.请有人给我一些见解.
谢谢!
Vla*_*kin 73
从'Gingerbread'开始,您可以使用IConnectivityManager.setMobileDataEnabled()方法.它隐藏在API中,但可以通过反射访问. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/net/ConnectivityManager.java#376
使用此方法,您可以更改系统设置:'设置 - >无线和网络 - >移动网络设置 - > 数据已启用 '
代码示例:
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);
}
Run Code Online (Sandbox Code Playgroud)
您还需要CHANGE_NETWORK_STATE权限.
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
Run Code Online (Sandbox Code Playgroud)
不用说,这种方法可能在未来的Android版本中不起作用.但我想像"3G看门狗","APNdroid"或"DataLock"这样的应用就是这样的.
更新:
Lollipop上不再提供setMobileDataEnabled方法
Ind*_*dra 38
Dataconnection禁用和启用APIS隐藏在SDK中而不会暴露给用户,这可以通过使用java反射技术访问ITelephony接口来实现.
干得好:
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
isEnabled = true;
}else{
isEnabled = false;
}
telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
if (isEnabled) {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("disableDataConnectivity");
} else {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("enableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
89168 次 |
| 最近记录: |