利用反射Android开发之开启关闭数据连接实现
来源:互联网 发布:php和java发展 编辑:程序博客网 时间:2024/05/23 13:11
使用的是java的反射机制.直接上代码:
http://www.jsqj.cc 军事新闻
private TextView mTextView = null;
private Button mOpenMoblieNet = null;
private boolean isEnabled = false;
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.show_mobile_state);
mOpenMoblieNet = (Button) findViewById(R.id.open_mobile_network);
telephonyManager = (TelephonyManager) getApplicationContext()
.getSystemService(Context.TELEPHONY_SERVICE);
mOpenMoblieNet.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
try
{
// 获取当前的状态
if (telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED)
{
isEnabled = true;
mTextView.setText("数据连接已开启");
} else
{
isEnabled = false;
mTextView.setText("数据连接已关闭");
}
Log.i("debugs", "当前数据连接是:" + isEnabled);
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");
mTextView.setText("数据连接已关闭");
Log.i("debugs", "当前数据连接是:" + isEnabled);
} else
{
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("enableDataConnectivity");
mTextView.setText("数据连接已打开");
Log.i("debugs", "当前数据连接是:" + isEnabled);
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
0 0
- 利用反射Android开发之开启关闭数据连接实现
- 利用反射来关闭开启GPRS
- Android 通过反射打开与关闭数据连接
- Android开发之控制Toast的开启与关闭
- Android开发之控制Toast的开启与关闭
- Android开发之ListView利用OnScrollListener实现分页加载数据
- Android开发之ListView利用OnScrollListener实现分页加载数据
- Android开发 之——利用RxBus实现数据更新
- Android利用代码开启关闭飞行模式
- Android源码开发之在下拉通知栏增加关闭数据连接的对话框
- Android开发--Service开启,关闭,绑定,解除
- 实现开启和关闭android移动网络
- 实现开启和关闭android移动网络
- 实现开启和关闭android移动网络
- Android 开启或关闭WIFI实现
- Android之开启关闭关软键盘
- 实现开启和关闭android移动网络(做AppWidget开发的收获)
- 实现开启和关闭Android移动网络(做AppWidget开发的收获)
- solr 服务端搭建
- 2013-2-17 春节玩游戏
- Mac,iOS界面中的三维坐标系
- 【半平面交】 HDOJ 1632 Polygons
- [ZOJ 2963] Treasure Hunter [最短路+状态压缩]
- 利用反射Android开发之开启关闭数据连接实现
- 第171天
- Memcache 和Redis
- Ubuntu samba
- 解决Http 中传参时的乱码问题
- 判断三维坐标系旋转正方向的简单方法
- String, StringBuffer与StringBuilder
- APK安装过程及原理详解
- XAMPP for MAC 创建Laravel工程