概述
平台:MTK
验证:Android 6.0
前提:需要获得系统签名
manifest中:需要添加如下2点:
1;android:sharedUserId="android.uid.phone"
2:<receiver
android:name="com.android.networkmode.NetworkMode"
android:process="com.android.phone" >
<intent-filter>
<action android:name="广播Action" />
</intent-filter>
</receiver>
类:广播
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.SystemProperties;
import android.os.UserHandle;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;
import android.os.AsyncResult;
import android.provider.Settings.Global;
import android.text.TextUtils;
import android.telephony.RadioAccessFamily;
import android.provider.Settings;
import android.util.Log;
import android.provider.Settings.System;
public class NetworkMode extends BroadcastReceiver {
private Context mContext;
private int mNetworkMode;
private Phone mPhone; private MyHandler mHandler;
private static final int preferredNetworkMode = Phone.PREFERRED_NT_MODE;
private static final String ACTION = "广播Action";
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
mPhone = PhoneFactory.getDefaultPhone();
mHandler = new MyHandler();
Log.d("test-net", "intent.getAction() = " + intent.getAction());
if (ACTION.equals(intent.getAction())) {
//判断是否能处于飞行模式
boolean isAirplaneMode = System.getInt(context.getContentResolver(),
System.AIRPLANE_MODE_ON, 0) != 0;
//获取要设置的网络制式
String modeType = intent.getStringExtra("modeType");
if (!isAirplaneMode) {
if ("2G".equals(modeType)) {
setPreferredNetworkType(1);// 2G网络
} else if ("3G".equals(modeType)) {
setPreferredNetworkType(0);// 3G网络
} else if ("4G".equals(modeType)) {
setPreferredNetworkType(9);// 4G网络
}
}
//设置飞行模式
if ("airplane_on".equals(mode)) {
setAirplaneModeOn(true);
} else if ("airplane_off".equals(mode)) {
setAirplaneModeOn(false);
}
}
}
//该部分从Settings中抽出取来使用的
private void setPreferredNetworkType(int networkMode) {
mNetworkMode = networkMode;
final int phoneSubId = mPhone.getSubId();
int settingsNetworkMode = Global.getInt(mContext.getContentResolver(),
Global.PREFERRED_NETWORK_MODE + phoneSubId,
preferredNetworkMode);
if (networkMode != settingsNetworkMode) {
Global.putInt(mContext.getContentResolver(),
Global.USER_PREFERRED_NETWORK_MODE + phoneSubId,
networkMode);
// Set the modem network mode
mPhone.setPreferredNetworkType(
networkMode,
mHandler.obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE));
}
}
/**
* Return whether the project is support WCDMA Preferred.
*
* @return If support, return true, else return false
*/
public static boolean isWCDMAPreferredSupport() {
String isWCDMAPreferred = SystemProperties
.get("ro.mtk_rat_wcdma_preferred");
if (TextUtils.isEmpty(isWCDMAPreferred)) {
return false;
}
return "1".equals(isWCDMAPreferred);
}
private class MyHandler extends Handler {
static final int MESSAGE_GET_PREFERRED_NETWORK_TYPE = 0;
static final int MESSAGE_SET_PREFERRED_NETWORK_TYPE = 1;
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_GET_PREFERRED_NETWORK_TYPE:
handleGetPreferredNetworkTypeResponse(msg);
break;
case MESSAGE_SET_PREFERRED_NETWORK_TYPE:
handleSetPreferredNetworkTypeResponse(msg);
break;
}
}
private void handleGetPreferredNetworkTypeResponse(Message msg) {
final int phoneSubId = mPhone.getSubId();
AsyncResult ar = (AsyncResult) msg.obj;
if (ar.exception == null) {
int modemNetworkMode = ((int[]) ar.result)[0];
int settingsNetworkMode = Global.getInt(mPhone.getContext()
.getContentResolver(), Global.PREFERRED_NETWORK_MODE
+ phoneSubId, preferredNetworkMode);
// check that modemNetworkMode is from an accepted value
if (modemNetworkMode == Phone.NT_MODE_WCDMA_PREF
|| modemNetworkMode == Phone.NT_MODE_GSM_ONLY
|| modemNetworkMode == Phone.NT_MODE_WCDMA_ONLY
|| modemNetworkMode == Phone.NT_MODE_GSM_UMTS
|| modemNetworkMode == Phone.NT_MODE_CDMA
|| modemNetworkMode == Phone.NT_MODE_CDMA_NO_EVDO
|| modemNetworkMode == Phone.NT_MODE_EVDO_NO_CDMA
|| modemNetworkMode == Phone.NT_MODE_GLOBAL
|| modemNetworkMode == Phone.NT_MODE_LTE_CDMA_AND_EVDO
|| modemNetworkMode == Phone.NT_MODE_LTE_GSM_WCDMA
|| modemNetworkMode == Phone.NT_MODE_LTE_CDMA_EVDO_GSM_WCDMA
|| modemNetworkMode == Phone.NT_MODE_LTE_ONLY
|| modemNetworkMode == Phone.NT_MODE_LTE_WCDMA) {
// Framework's Phone.NT_MODE_GSM_UMTS is same as app's
// NT_MODE_WCDMA_PREF, this is related with feature option
// MTK_RAT_WCDMA_PREFERRED. In app side, we should change
// the setting system's value to NT_MODE_WCDMA_PREF, and
// keep
// sync with Modem's value.
if (modemNetworkMode == Phone.NT_MODE_GSM_UMTS
&& isWCDMAPreferredSupport()) {
modemNetworkMode = Phone.NT_MODE_WCDMA_PREF;
if (settingsNetworkMode != Phone.NT_MODE_WCDMA_PREF) {
settingsNetworkMode = Phone.NT_MODE_WCDMA_PREF;
Global.putInt(mPhone.getContext()
.getContentResolver(),
Global.PREFERRED_NETWORK_MODE + phoneSubId,
settingsNetworkMode);
}
} else {
// check changes in modemNetworkMode
if (modemNetworkMode != settingsNetworkMode) {
settingsNetworkMode = modemNetworkMode;
}
}
} else {
resetNetworkModeToDefault();
}
}
}
private void handleSetPreferredNetworkTypeResponse(Message msg) {
AsyncResult ar = (AsyncResult) msg.obj;
final int phoneSubId = mPhone.getSubId();
if (ar.exception != null) {
mPhone.getPreferredNetworkType(obtainMessage(MESSAGE_GET_PREFERRED_NETWORK_TYPE));
} else if (isCapabilityPhone(mPhone)) {
int networkMode = mNetworkMode;
Global.putInt(mPhone.getContext().getContentResolver(),
Global.PREFERRED_NETWORK_MODE + phoneSubId, networkMode);
}
}
/**
* Is the phone has 3/4G capability or not.
*
* @return true if phone has 3/4G capability
*/
private boolean isCapabilityPhone(Phone phone) {
boolean result = phone != null ? ((phone.getRadioAccessFamily() & (RadioAccessFamily.RAF_UMTS | RadioAccessFamily.RAF_LTE)) > 0)
: false;
return result;
}
private void resetNetworkModeToDefault() {
final int phoneSubId = mPhone.getSubId();
Global.putInt(mPhone.getContext().getContentResolver(),
Global.PREFERRED_NETWORK_MODE + phoneSubId,
preferredNetworkMode);
mPhone.setPreferredNetworkType(
preferredNetworkMode,
this.obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE));
}
}
private void setAirplaneModeOn(boolean enabling) {
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
enabling ? 1 : 0);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", enabling);
mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
}
}
最后
以上就是失眠饼干为你收集整理的MTK平台 切换网络制式的全部内容,希望文章能够帮你解决MTK平台 切换网络制式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复