若是有這方面需求能夠參考。html
利用JAVA的反射機制(Reflection),來調用CONNECTIVITY_SERVICE完成相關操做。java
關於JAVA的反射機制,能夠參考百度百科或者如下文章:android
JAVA反射機制 :http://blog.csdn.net/justinavril/article/details/2873664app
java反射詳解 :http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.htmlide
首先要添加權限spa
改變狀態要添加 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 訪問數據要添加 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在視圖中只須要添加一個按鈕充當開關:.net
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="144dp" android:text="打開/關閉 GPRS" /> </RelativeLayout>
在JAVA文件中利用反射機制打開或者關閉數據流量:code
package com.example.opengprs; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.app.Activity; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button OnOff = (Button) findViewById(R.id.button1); OnOff.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Method dataConnSwitchmethod; Class connManagerClass; Object IConnStub; Class IConnClass; boolean isEnable; NetworkInfo mNetworkInfo; ConnectivityManager ConnManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); mNetworkInfo=ConnManager.getActiveNetworkInfo(); if(mNetworkInfo != null){ if(mNetworkInfo.isAvailable()==true) isEnable = true; else isEnable = false; }else{ isEnable = false; } try { connManagerClass = Class.forName(ConnManager.getClass().getName()); Method m[]=connManagerClass.getDeclaredMethods(); for(int i=0;i<m.length;i++) Log.d("OnOff", m[i].toString()); Field getITelephonyMethod = connManagerClass.getDeclaredField("mService"); getITelephonyMethod.setAccessible(true); IConnStub = getITelephonyMethod.get(ConnManager); IConnClass = Class.forName(IConnStub.getClass().getName()); dataConnSwitchmethod=IConnClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); dataConnSwitchmethod.setAccessible(true); dataConnSwitchmethod.invoke(IConnStub,!isEnable); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }
這樣就能夠在APP裏打開或關閉數據流量了。xml