android移动数据开发:主要包含移动数据强度、移动数据类型、手机是否飞行模式 、设置手机飞行模式、SIM卡的状态监听几大主要模块。
移动数据强度:根据信号范围定义相关的信号格数
移动数据类型:移动数据 2G 3G 4G 类型 以及各类型之间切换
飞行模式:飞行模式设置和飞行模式状态监听
SIM卡状态:SIM状态监听和SIM插拔
在Android中对移动数据操作 android 主要提供以下几个类和接口:
TelephonyManager类主要提供了一系列用于访问与手机通讯相关的状态和信息的get方法。其中包括手机SIM的状态和信息、电信网络的状态及手机用户的信息。在应用程序中可以使用这些get方法获取相关数据。
TelephonyManager类的对象可以通过Context.getSystemService(Context.TELEPHONY_SERVICE)方法来获得,需要注意的是有些通讯信息的获取对应用程序的权限有一定的限制,在开发的时候需要为其添加相应的权限。
PhoneStateListener除了可以监听手机的通话状态,数据连接活动状态,数据连接状态还可以监听很多状态。
PhoneStateListener类方法: (移动数据强度和类型 在打电、SIM卡插拔、连接状态等状态改变而受影响,因此需要在状态下分别检测数据强度和类型 监听回调)
1.onDataActivity(intdirection) :activity 数据状态改变时监听回调
2.onServiceStateChanged(ServiceState serviceState) {//设备服务状态改变监听回调
3.onSignalStrengthsChanged(SignalStrength signalStrength) {//信号强度改变监听回调
4.onDataConnectionStateChanged(intstate) {//数据连接状态
5.onCallStateChanged(intstate, String incomingNumber) {//电话状态改变
移动数据代码如下:
/**
* 0--4
*/
privateintmLevel=0;
/**
*
*/
privateintmState= 0 ;
/**
* 1--2G 2--3G 3--4G 0--unknow
*/
private int mNetType= 0 ;
private TelephonyManagertel;
private Contextcontext=null;
private SignalChangeCallback mSignalChangedCallback=null;
public SignalManager (Context context){
this.context= context;
tel=(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
tel.listen(newMyPhoneStateListener(),
PhoneStateListener.LISTEN_SERVICE_STATE
|PhoneStateListener.LISTEN_SIGNAL_STRENGTHS
| PhoneStateListener.LISTEN_DATA_ACTIVITY
| PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
}
private SignalStrength mSignalStrength=null;//获取信号强度类
public void setSignalChangedCallback(SignalChangeCallback callback){
mSignalChangedCallback= callback;
}
class MyPhoneStateListener extends PhoneStateListener{
@Override
public void onDataActivity(intdirection) {//当activity 数据状态改变时监听回调
super.onDataActivity(direction);
switch(direction){
case TelephonyManager.DATA_ACTIVITY_IN:
break;
case TelephonyManager.DATA_ACTIVITY_INOUT:
break;
case TelephonyManager.DATA_ACTIVITY_OUT:
break;
case TelephonyManager.DATA_ACTIVITY_NONE:
break;
}
if(mSignalChangedCallback!=null){//监听回调
mSignalChangedCallback.onDataActivityChange(direction);
}
}
@Override
public void onServiceStateChanged(ServiceState serviceState) {//设备服务状态改变监听回调
super.onServiceStateChanged(serviceState);
int state =serviceState.getState();
switch(state){
case ServiceState.STATE_EMERGENCY_ONLY:
mState=0x01;
break;
case ServiceState.STATE_IN_SERVICE:
mState=0x02;
break;
case ServiceState.STATE_OUT_OF_SERVICE:
mState=0x03;
break;
case ServiceState.STATE_POWER_OFF:
mState= 0x04;
break;
default:
break;
}
mNetType=tel.getNetworkClass(tel.getNetworkType());
if(mNetType!=0){
if(mNetType+1==4){
if(mSignalStrength!=null){
mLevel= getGsmLevel(mSignalStrength);//获取移动数据信号强度
}
}elseif(mNetType== 2 ||mNetType== 1){
if(mSignalStrength!=null){
mLevel= getGsmLevel(mSignalStrength);//获取移动数据信号强度
}
}
}else{
if(mSignalStrength!=null){
mLevel= getGsmLevel(mSignalStrength);//获取移动数据信号强度
}
}
if(mSignalChangedCallback!=null){//监听回调移动数据类型
mSignalChangedCallback.onNetTypeChange(mNetType);
}
if(mSignalChangedCallback!=null){//监听回调移动数据强度
mSignalChangedCallback.onSignalChange(mLevel);
}
}
publicintgetGsmLevel(SignalStrength signalStrength){
intlevel = 0;
intasu = signalStrength.getGsmSignalStrength();
if(asu <= 2||asu ==99) level =5;
elseif(asu >=19) level =5;
elseif(asu >=17) level =4;
elseif(asu >=14) level =3;
elseif(asu >=10) level =2;
elseif(asu >=8) level =1;
return level;
}
@Override
publicvoidonSignalStrengthsChanged(SignalStrength signalStrength) {//信号强度改变监听回调
//TODOAuto-generated method stub
super.onSignalStrengthsChanged(signalStrength);
mSignalStrength= signalStrength;
mNetType=tel.getNetworkClass(tel.getNetworkType());
if(mNetType+1==4){
if(mSignalStrength!=null){
mLevel= getGsmLevel(mSignalStrength);//获取移动数据信号强度
}
}elseif(mNetType== 2 ||mNetType== 1){
if(mSignalStrength!=null){
mLevel= getGsmLevel(mSignalStrength);//获取移动数据信号强度
}
}
if(mNetType!=0){
mLevel= getGsmLevel(mSignalStrength);
}else{
if(mSignalStrength!=null){
mLevel= getGsmLevel(mSignalStrength);//获取移动数据信号强度
}
if(mSignalChangedCallback!=null){//监听回调移动数据强度
mSignalChangedCallback.onSignalChange(mLevel);
}
}
if(mSignalChangedCallback!=null){//监听回调移动数据类型
mSignalChangedCallback.onNetTypeChange(mNetType);
}
if(mSignalChangedCallback!=null){//监听回调移动数据强度
mSignalChangedCallback.onNetTypeChange(mNetType);
mSignalChangedCallback.onSignalChange(mLevel);
}
}
@Override
publicvoidonDataConnectionStateChanged(intstate) {//数据连接状态
//TODOAuto-generated method stub
super.onDataConnectionStateChanged(state);
switch(state){
caseTelephonyManager.DATA_CONNECTED://
break;
caseTelephonyManager.DATA_CONNECTING:
break;
caseTelephonyManager.DATA_DISCONNECTED:
break;
caseTelephonyManager.DATA_SUSPENDED:
break;
}
mNetType=tel.getNetworkClass(tel.getNetworkType());
if(mNetType!=0){
if(mNetType+1==4){
if(mSignalStrength!=null){
mLevel= getGsmLevel(mSignalStrength);//获取移动数据信号强度
}
}elseif(mNetType== 2 ||mNetType== 1){
if(mSignalStrength!=null){
mLevel= getGsmLevel(mSignalStrength);//获取移动数据信号强度
}
}
}else{
if(mSignalStrength!=null){
mLevel= getGsmLevel(mSignalStrength);//获取移动数据信号强度
}
if(mSignalChangedCallback!=null){//监听回调移动数据强度
mSignalChangedCallback.onSignalChange(mLevel);
}
}
if(mSignalChangedCallback!=null){//监听回调移动数据类型
mSignalChangedCallback.onNetTypeChange(mNetType);
}
}
@Override
publicvoidonCellLocationChanged(CellLocation location) {//电话位置改变
//TODOAuto-generated method stub
super.onCellLocationChanged(location);
}
@Override
publicvoidonCallStateChanged(intstate, String incomingNumber) {//电话状态改变
//TODOAuto-generated method stub
super.onCallStateChanged(state, incomingNumber);
mNetType=tel.getNetworkClass(tel.getNetworkType());
switch(state){
caseTelephonyManager.CALL_STATE_IDLE:
//挂断
break;
caseTelephonyManager.CALL_STATE_OFFHOOK:
//接听
break;
caseTelephonyManager.CALL_STATE_RINGING:
//来电状态
break;
}
mNetType=tel.getNetworkClass(tel.getNetworkType());
if(mNetType!=0){
if(mNetType+1==4){
if(mSignalStrength!=null){
mLevel= getGsmLevel(mSignalStrength);//获取移动数据信号强度
}
}elseif(mNetType== 2 ||mNetType== 1){
if(mSignalStrength!=null){
mLevel= getGsmLevel(mSignalStrength);//获取移动数据信号强度
}
}
}else{
if(mSignalStrength!=null){
mLevel= getGsmLevel(mSignalStrength);//获取移动数据信号强度
}
if(mSignalChangedCallback!=null){//监听回调移动数据强度
mSignalChangedCallback.onSignalChange(mLevel);
}
}
if(mSignalChangedCallback!=null){//监听回调移动数据类型
mSignalChangedCallback.onNetTypeChange(mNetType);
}
}
}
publicinterfaceSignalChangeCallback{
voidonStateChange(intstate);
voidonSignalChange(intlevel);
voidonNetTypeChange(intnetType);
voidonDataActivityChange(intdirction);
voidgpsLocation(booleanb);
}
判断飞行模式状态和设置飞行模式代码:
/**
* 设置手机飞行模式
*@authorZH-SW-Weiw
*
*/
publicvoidsetAirplaneModeOn(booleanenabling){
Settings.System.putInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, enabling ? 1 : 0);
Intent intent =newIntent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", enabling);
context.sendBroadcast(intent);
}
/**
* 判断手机是否是飞行模式
*@authorZH-SW-Weiw
*
*/
publicbooleanisAirplaneMode(){
intisAirplaneMode = Settings.System.getInt(
context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
0);
return(isAirplaneMode == 1) ?true:false;
}
设置移动数据开关和监听移动数据开关状态代码:注在设置移动数据开关时候要判断SIM状态(
(telephonyManager.getSimState() == TelephonyManager.SIM_STATE_ABSENT
||telephonyManager.getSimState() == TelephonyManager.SIM_STATE_UNKNOWN)
/**
* 设置移动数据状态
*@paramcontext
*@parammobileDataEnabled
*/
publicvoidsetMobileDataState(Context context,booleanisEnable) {
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
Method methodSet;
try{
methodSet = Class.forName(tm.getClass().getName()).getDeclaredMethod("setDataEnabled", Boolean.TYPE);
methodSet.invoke(tm,isEnable);
}catch(Exception e) {
e.printStackTrace();
Log.d(TAG," e.toString"+e.toString());
}
}
/**
* 获取数据开关状态
*@paramcontext
*@paramgetMobileData
*@return
*/
publicbooleangetMobileDataStatus(Context context){
ConnectivityManager mConnectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
String methodName ="getMobileDataEnabled";
ClasscmClass = mConnectivityManager.getClass();
Boolean isOpen =null;
try
{
Method method =cmClass.getMethod(methodName,null);
isOpen = (Boolean)method.invoke(mConnectivityManager,null);
}
catch(Exception e)
{
e.printStackTrace();
Log.d(TAG," e.toString"+e.toString());
}
returnisOpen;
}