首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

如何用代码实现移动网络数据访问功能

2012-07-08 
怎么用代码实现移动网络数据访问功能大家好,本人刚刚接触Andorid,很多地方不懂,希望大侠多多赐教,不胜感激

怎么用代码实现移动网络数据访问功能
大家好,本人刚刚接触Andorid,很多地方不懂,希望大侠多多赐教,不胜感激。

现请教一个问题:手机是使用电信3G网络的,请问怎么用代码实现移动网络数据访问功能(即设置里面的选项开关"启动移动数据"的打开与关闭)。

分数不多了,希望大家帮忙,非常感谢。

[解决办法]
基本上就是这个代码,但是好多都不行,呵呵!

Java code
@SuppressWarnings("unchecked")    public void openDataConnect() throws ClassNotFoundException,            SecurityException, NoSuchMethodException, IllegalArgumentException,            IllegalAccessException, InvocationTargetException {        Method dataConnSwitchmethod;        Class telephonyManagerClass;        Object ITelephonyStub;        Class ITelephonyClass;        boolean isEnabled = false;                TelephonyManager telephonyManager = (TelephonyManager) context.get()                .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);    }
[解决办法]
再frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\tablet\SettingsView.java路径下

public class SettingsView extends LinearLayout implements View.OnClickListener{
//增加数据广播实例声明
DataEnabledController mDataEnabled;
...
  
//在该方法下增加~
protected void onFinishInflate() {
...
mDataEnabled = new DataEnabledController(context,
(CompoundButton)findViewById(R.id.dataEnable_checkbox));
}

}


再在SystemUI\res下相应的xml布局文件内加入
<LinearLayout
android:id="@+id/dataEnable"
style="@style/StatusBarPanelSettingsRow"
>
<ImageView
android:id="@+id/dataEnable_icon"
style="@style/StatusBarPanelSettingsIcon"
android:src="@drawable/ic_sysbar_data_switcher"
/>
<TextView
android:id="@+id/dataEnable_label"
style="@style/StatusBarPanelSettingsContents"
android:text="@string/status_bar_settings_data_enabled"
/>
<Switch
android:id="@+id/dataEnable_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
/>
</LinearLayout>
<View style="@style/StatusBarPanelSettingsPanelSeparator" />


最后在SystemUI\res下加入相应资源图片和字符串即可~ 希望对你有所帮助~

热点排行