您的位置:首页 > 移动开发 > Android开发

android 各种系统设置的操作

2016-11-06 10:18 781 查看
以下是android5.1的相关设置

1,飞行模式的设置 

private int mModel;
private static final int AIRPLANE_OFF= 0; // 飞行模式关闭
private static final int AIRPLANE_ON = 1;  // 飞行模式打开


一般默认情况下,飞行模式是关闭的。

获取飞行模式的状态,

mModel = Settings.Global.getInt(getContentResolver(),Settings.Global.AIRPLANE_MODE_ON, AIRPLANE_OFF);


设置飞行模式的状态,

Settings.Global.putInt(getContentResolver(),Settings.Global.AIRPLANE_MODE_ON, mModel);


设置完了之后不要忘记发出飞行模式改变了的粘性广播,因为很多地方需要根据飞行模式的状态来调整行为。

Intent intent = new Intent("android.action.SETTING_AIRPLANE_MODE_UPDATE");
intent.putExtra("state", mModel == AIRPLANE_ON ? true : false);
sendStickyBroadcast(intent);


 

2,数据业务的开/关设置 

首先判断是否有在使用数据业务

private TelephonyManager sTelephonyManager = null;
private SubscriptionInfo sir;
sTelephonyManager  =(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
sir = getsir();
if (!sTelephonyManager.getDataEnabled()) {
// 说明未使用数据业务
} else if (sir != null) {
// 说明当前正在使用数据业务
}

private SubscriptionInfo getsir() {
int subId = SubscriptionManager.getDefaultDataSubId();
final List<SubscriptionInfo> subInfoList =
SubscriptionManager.from(this).getActiveSubscriptionInfoList();
if (subInfoList != null) {
final int subInfoLength = subInfoList.size();
for (int i = 0; i < subInfoLength; ++i) {
final SubscriptionInfo sir = subInfoList.get(i);
if (sir != null && sir.getSubscriptionId() == subId) {
return sir;
}
}
}

return null;
}


设置数据业务是否可用,调用以下方法就可以了;输入参数enabled为true表示打开数据业务,否则关闭数据业务,

 

private void setMobileDataEnabled(boolean enabled) {
if (sTelephonyManager != null) {
int[] sub1Id = SubscriptionManager.getSubId(PhoneConstants.SUB1);
sTelephonyManager.setDataEnabled(sub1Id[0], enabled);

int[] sub2Id = SubscriptionManager.getSubId(PhoneConstants.SUB2);
sTelephonyManager.setDataEnabled(sub2Id[0], enabled);
}
}


当然,前提必须得有张SIM卡才可以使用数据业务。

 

3,语言设置 

获取系统中所有的语言,存放在list中,

private List<LocalePicker.LocaleInfo> locales;
locales = LocalePicker.getAllAssetLocales(this, false); // 获取所有语言
Collections.sort(locales); // 按照字母进行排序


比如现在想将语言设置为中文,在该list中,已知中文是第30个,

 那么可以直接调用updatelanguge(29)完成语言的设置。

public void updatelanguge(int position) {
int mm = 0;
LocalePicker.LocaleInfo mlacal = null;
for (LocalePicker.LocaleInfo locale : locales) {
mlacal = locale;
if(mm == position){
break;
}
mm++;
}
final Locale locale = mlacal.getLocale();
LocalePicker.updateLocale(locale);
}


4,恢复出厂设置 

恢复出厂设置直接发送一个广播就可以了,

Intent intent = new Intent("android.intent.action.MASTER_CLEAR");
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
sendBroadcast(intent);


最后,不要忘记了,在AndroidManifest.xml 文件中添加对应的权限。

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息