android 自动打开移动网络,android 打开3G 移动网络

package com.example.test;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import android.net.ConnectivityManager;

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends Activity {

public Button open3g;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

open3g=(Button)findViewById(R.id.open3G);

open3g.setOnClickListener(new open3GClickListener());

}

class open3GClickListener implements OnClickListener{

@Override

public void onClick(View v) {

setMobileNetEnable();

Toast.makeText(MainActivity.this,"hello", 1).show();

}

}

/**    * 移动网络开关    */

//    private void toggleMobileData( boolean enabled) {

//ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

//

//Class> conMgrClass = null;

ConnectivityManager类

//Field iConMgrField = null;

ConnectivityManager类中的字段

//Object iConMgr = null;

IConnectivityManager类的引用

//Class> iConMgrClass = null;

IConnectivityManager类

//Method setMobileDataEnabledMethod = null;

setMobileDataEnabled方法

//try {

取得ConnectivityManager类

//conMgrClass = Class.forName(conMgr.getClass().getName());

取得ConnectivityManager类中的对象mService

//iConMgrField = conMgrClass.getDeclaredField("mService");

设置mService可访问

//iConMgrField.setAccessible(true);

取得mService的实例化类IConnectivityManager

//iConMgr = iConMgrField.get(conMgr);

取得IConnectivityManager类

//iConMgrClass = Class.forName(iConMgr.getClass().getName());

取得IConnectivityManager类中的setMobileDataEnabled(boolean)方法

//setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);

设置setMobileDataEnabled方法可访问

//setMobileDataEnabledMethod.setAccessible(true);

调用setMobileDataEnabled方法

//setMobileDataEnabledMethod.invoke(iConMgr, enabled);

//} catch (ClassNotFoundException e) {

//e.printStackTrace();

//}

//catch (NoSuchFieldException e) {

//    e.printStackTrace();

//}

//catch (SecurityException e) {

//e.printStackTrace();

//}

//catch (NoSuchMethodException e) {

// e.printStackTrace();

//}

//catch (IllegalArgumentException e) {

// e.printStackTrace();

//}

//catch (IllegalAccessException e) {

// e.printStackTrace();

//}

//catch (InvocationTargetException e) {

//e.printStackTrace();

//}

//

//}

///

//

public final void setMobileNetEnable(){

ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

Object[] arg = null;

try {

boolean isMobileDataEnable = invokeMethod("getMobileDataEnabled", arg);

if(!isMobileDataEnable){

invokeBooleanArgMethod("setMobileDataEnabled", true);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public boolean invokeMethod(String methodName,

Object[]  arg) throws Exception {

ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

Class ownerClass = mConnectivityManager.getClass();

Class[]  argsClass = null;

if (arg != null) {

argsClass = new Class[1];

argsClass[0] = arg.getClass();

}

Method method = ownerClass.getMethod(methodName, argsClass);

Boolean isOpen = (Boolean) method.invoke(mConnectivityManager, arg);

return isOpen;

}

public Object invokeBooleanArgMethod(String methodName,

boolean value) throws Exception {

ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

Class ownerClass = mConnectivityManager.getClass();

Class[]  argsClass = new Class[1];

argsClass[0]=boolean.class;

Method method = ownerClass.getMethod(methodName,argsClass);

return method.invoke(mConnectivityManager, value);

}

//

}

f5eb4426879d9c4b4b2deb15679e0746.pngTest.rar

阅读(620) | 评论(0) | 转发(0) |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值