1.获取注册的网络信息
TelephonyManager tm= (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
Lst<CellInfo> cellinfos = tm.getAllCellInfo();
// androi10之前需要权限ACCESS_COARSE_LOCATION,andrid10开始需要ACCESS_FINE_LOCATION权限,android13上还需要PackageManager.FEATURE_TELEPHONY_RADIO_ACCESS权限
//在phoneInterfaceManager中进行权限判断
for(CellInfo cellinfo : cellinfos){
if(cellinfo.isRegistered()){//判断是否已经注册
//CellIdentityGsm ,CellIdentityCdma,CellIdentityTdscdma,CellIdentityWcdma,CellIdentityLte,CellIdentityNr
// cellIdentity 中包含mcc,mnc,tac,mci等信息
CellIdentity cellIdentity =cellinfo.getCellIdentity();
//同样有Gsm,Cdma,TdScdma,Wcdma,Lte,Nr等 ,cellSignalStrength中包含Rssi,Rssnr,Rsrp,Dbm等信息
CellSignalStrength cellSignalStrength=cellinfo.getCellSignalStrength();
break;
}
}
2.激活去激活卡一卡二
需要使用telephony-ext.jar ,使用系统app或者Settings添加广播接收器
//激活
IExtTelephony extTelephony = IExtTelephony.Stub.asInterface(ServiceManager.getService("extphone"));
result = extTelephony.activateUiccCard(slotId);
//去激活
IExtTelephony extTelephony = IExtTelephony.Stub.asInterface(ServiceManager.getService("extphone"));
result = extTelephony.deactivateUiccCard(slotId);
3.usb状态获取及模式切换
public void setCurrentFunctions(long function){
//function: 0代表默认,不用于数据传输,4代表MTP,8代表MIDI,16代表PTP,32代表rndis
UsbManager usb = (UsbManager) getSystemService(USB_SERVICE);
usb.setCurrentFunctions(function); //@hide修饰,三方app需使用反射
}
public String getCurrentConfig(){
String value = "test";
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class, String.class );
value = (String)(get.invoke(c, "sys.usb.config", "unknown" ));
} catch (Exception e) {
e.printStackTrace();
}finally {
return value;
}
}
4.关闭打开数据业务
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
tm.setDataEnabled(false) ;//关闭 ,需要MODIFY_PHONE_STATE权限
tm.setDataEnabled(true); //打开
5.读取IMEI
TelephonyManager tm =(TelephonyManager) getSystemService(TELEPHONY_SERVICE);
//androd10之前获取READ_PHONE_STATE权限,从android10开始是READ_PRIVILEGED_PHONE_STATE权限,android13上还要求PackageManager.FEATURE_TELEPHONY_GSM
String imei1= tm.getImei(0);
String imei2= tm.getImei(1);
6.wifi,4G之间切换
INetworkManagementService为NetworkManagementService,需使用services.jar
Network currentNetwork=ConnectivityManager.from(this).getActiveNetwork(); //获取当前激活的网络
Network mobileNetwork,wifiNetwork;
Network[] networks= ConnectivityManager.from(this).getAllNetworks(); //获取所有网络
for(Network network :networks){
NetworkInfo networkinfo =ConnectivityManager.from(this).getNetworkInfo(network); //获取网络信息
if(networkinfo.getTypeName().equals("Mobile")&&!networkinfo.getExtraInfo().equals("ims")) { //4G
mobileNetwork = network ;
}
if(networkinfo.getTypeName().equals("Mobile")){
wifiNetwork =network;
}
}
//清空
final INetworkManagementService nms = INetworkManagementService.Stub.asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
nms.clearDefaultNetId();
//切换到4G
int netId= mobileNetwork.netId;
nms.setNetworkPermission(netId,"NETWORK");
nms.setDefaultNetId(netId);
nms.setNetworkPermission(netID,"");
7.设置默认数据卡,电话卡,短信卡
//默认数据卡
public void setDefaultDataSubId(final int subId) {
final SubscriptionManager subscriptionManager = SubscriptionManager.from(mContext);
final TelephonyManager telephonyManager = TelephonyManager.from(mContext);
subscriptionManager.setDefaultDataSubId(subId);
telephonyManager.setDataEnabled(true);
}
//默认电话卡
public void setDefaultCallsSubId(final int subId) {
final PhoneAccountHandle phoneAccount = subscriptionIdToPhoneAccountHandle(subId);
final TelecomManager telecomManager = TelecomManager.from(mContext);
telecomManager.setUserSelectedOutgoingPhoneAccount(phoneAccount);
}
//默认短信卡
public void setDefaultSmsSubId(final int subId) {
Log.d(TAG,"setDefaultSmsSubId");
final SubscriptionManager subscriptionManager = SubscriptionManager.from(mContext);
subscriptionManager.setDefaultSmsSubId(subId);
}
public PhoneAccountHandle subscriptionIdToPhoneAccountHandle(final int subId) {
final TelecomManager telecomManager = TelecomManager.from(mContext);
final TelephonyManager telephonyManager = TelephonyManager.from(mContext);
for (PhoneAccountHandle handle : telecomManager.getCallCapablePhoneAccounts()) {
final PhoneAccount phoneAccount = telecomManager.getPhoneAccount(handle);
if (subId == telephonyManager.getSubIdForPhoneAccount(phoneAccount)) {
return handle;
}
}
return null;
}
8.恢复出厂设置
public void FactoryReset(){
PersistentDataBlockManager pdbManager = (PersistentDataBlockManager) getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
pdbManager.wipe();
Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
intent.setPackage("android");
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, false);
intent.putExtra(Intent.EXTRA_WIPE_ESIMS, false);
sendBroadcast(intent);
}
9.开关飞行模式
import android.net.ConnectivityManager;
public void setAirplaneMode(boolean b){
ConnectivityManager.from(this).setAirplaneMode(b);
}