手机放入sim卡后可以对apn进行管理,主要是数据库的操作。由于当时要做的是一个IPC的方案,所以为了解决aidl对参数类的限制,需要自定义如下的aidl类型:
package com.xxx.service;
import android.os.Parcel;
import android.os.Parcelable;
public class ApnInfo implements Parcelable {
private String apnName;
private String apn;
private String userName;
private String password;
private String proxy;
private String port;
public ApnInfo(){
}
public ApnInfo(Parcel parcel){
readFromParcel(parcel);
}
public String getApnName() {
return apnName;
}
public void setApnName(String apnName) {
this.apnName = apnName;
}
public String getApn() {
return apn;
}
public void setApn(String apn) {
this.apn = apn;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getProxy() {
return proxy;
}
public void setProxy(String proxy) {
this.proxy = proxy;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
@Override
public String toString() {
return "ApnInfo [apnName=" + apnName + ", apn=" + apn + ", userName="
+ userName + ", password=" + password + ", proxy=" + proxy
+ ", port=" + port + "]";
}
private void readFromParcel(Parcel parcel) {
// TODO Auto-generated method stub
this.apnName=parcel.readString();
this.apn=parcel.readString();
this.userName=parcel.readString();
this.password=parcel.readString();
this.proxy=parcel.readString();
this.port=parcel.readString();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(this.apnName);
dest.writeString(this.apn);
dest.writeString(this.userName);
dest.writeString(this.password);
dest.writeString(this.proxy);
dest.writeString(this.port);
}
public static final Parcelable.Creator<ApnInfo> CREATOR = new Creator<ApnInfo>() {
@Override
public ApnInfo[] newArray(int size) {
// TODO Auto-generated method stub
return new ApnInfo[size];
}
@Override
public ApnInfo createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new ApnInfo(source);
}
};
}
然后定义一个同名的aidl类,将其声明为可在aidl中进行传递的类:
package com.xxx.service;
parcelable ApnInfo;
之后,我们就可以直接在aidl中传递这个自定义类型ApnInfo,最后就是相应service的实现:
private static final Uri APN_URI = Uri.parse("content://telephony/carriers");
private static final Uri PREFERAPN_URI = Uri.parse("content://telephony/carriers/preferapn");
private Uri getUriForCurrSubId(Uri uri) {
SubscriptionInfo mSubscriptionInfo = SubscriptionManager.from(mContext).getActiveSubscriptionInfo(SubscriptionManager.getDefaultDataSubscriptionId());
int subId = mSubscriptionInfo != null ? mSubscriptionInfo.getSubscriptionId()
: SubscriptionManager.INVALID_SUBSCRIPTION_ID;
if (SubscriptionManager.isValidSubscriptionId(subId)) {
return Uri.withAppendedPath(uri, "subId/" + String.valueOf(subId));
} else {
return uri;
}
}
private String getSelectedApnKey() {
String key = null;
Cursor cursor = mContext.getContentResolver().query(getUriForCurrSubId(PREFERAPN_URI),
new String[] {"_id"}, null, null, null);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
key = cursor.getString(0);
}
cursor.close();
return key;
}
//添加APN信息
public boolean addApn(ApnInfo apnInfo){
// TODO Auto-generated method stub
Log.d(TAG,"start addApn");
TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
String numeric=tm.getSubscriberId().substring(0, 5);
ContentValues values = new ContentValues();
values.put("name", apnInfo.getApnName());
values.put("apn", apnInfo.getApn());
values.put("type","default");
values.put("numeric",numeric);
values.put("mcc", numeric.substring(0, 3));
values.put("mnc", numeric.substring(3, 5));
values.put("proxy", apnInfo.getProxy());
values.put("port", apnInfo.getPort());
values.put("mmsproxy","");
values.put("mmsport","");
values.put("user",apnInfo.getUserName());
values.put("server","");
values.put("password",apnInfo.getPassword());
values.put("mmsc","");
Uri newUri = mContext.getContentResolver().insert(APN_URI, values);
if (newUri != null){
Log.d(TAG, "addApn: " + newUri);
return true;
}
Log.d(TAG, "addApn failed");
return false;
}
//删除APN信息
public boolean deleteApn(String apnName){
// TODO Auto-generated method stub
Log.d(TAG,"start deleteApn");
int count=mContext.getContentResolver().delete(APN_URI,"name=?",new String[]{apnName});
return count>0;
}
//获得当前的APN信息
public ApnInfo getCurrApn(){
Log.d(TAG,"start getCurrApn");
ApnInfo apnInfo=new ApnInfo();
int pos = Integer.parseInt(getSelectedApnKey());
Uri PreferApnUri = ContentUris.withAppendedId(APN_URI, pos);
Cursor cr = mContext.getContentResolver().query(PreferApnUri, null, null, null, null);
Log.d(TAG, "getCurrentApn : cr=" + cr);
while(cr!=null && cr.moveToNext()){
apnInfo.setApnName(cr.getString(cr.getColumnIndex("name")));
apnInfo.setApn(cr.getString(cr.getColumnIndex("apn")));
apnInfo.setUserName(cr.getString(cr.getColumnIndex("user")));
apnInfo.setPassword(cr.getString(cr.getColumnIndex("password")));
apnInfo.setProxy(cr.getString(cr.getColumnIndex("proxy")));
apnInfo.setPort(cr.getString(cr.getColumnIndex("port")));
}
if(cr!=null){
cr.close();
}
return apnInfo;
}
//设置当前的APN
public boolean setCurrApn(String apnName){
Log.d(TAG,"start setCurrApn");
int count=-1;
ContentResolver resolver = mContext.getContentResolver();
Cursor cursor= mContext.getContentResolver().query(APN_URI,new String[] {"_id"},"name=?",new String[]{apnName},null);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
ContentValues values = new ContentValues();
values.put("apn_id", cursor.getInt(0));
count=resolver.update(getUriForCurrSubId(PREFERAPN_URI), values, null, null);
}
if (cursor != null) {
cursor.close();
}
return count>0;
}
//查询所有已有的APN信息
public List<ApnInfo> getAllApn(){
Log.d(TAG,"start getAllApn");
List<ApnInfo> list = new ArrayList();
CarrierConfigManager configManager = (CarrierConfigManager)
mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
PersistableBundle b = configManager.getConfig();
boolean mHideImsApn = b.getBoolean(CarrierConfigManager.KEY_HIDE_IMS_APN_BOOL);
final TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
final String mccmnc = tm.getSubscriberId().substring(0, 5);
Log.d(TAG, "mccmnc = " + mccmnc);
StringBuilder where = new StringBuilder("numeric=\"" + mccmnc +
"\" AND NOT (type='ia' AND (apn=\"\" OR apn IS NULL)) AND user_visible!=0");
if (mHideImsApn) {
where.append(" AND NOT (type='ims')");
}
Cursor cr = mContext.getContentResolver().query(APN_URI, null, where.toString(), null, null);
while (cr != null && cr.moveToNext()) {
ApnInfo apnInfo=new ApnInfo();
apnInfo.setApnName(cr.getString(cr.getColumnIndex("name")));
apnInfo.setApn(cr.getString(cr.getColumnIndex("apn")));
apnInfo.setUserName(cr.getString(cr.getColumnIndex("user")));
apnInfo.setPassword(cr.getString(cr.getColumnIndex("password")));
apnInfo.setProxy(cr.getString(cr.getColumnIndex("proxy")));
apnInfo.setPort(cr.getString(cr.getColumnIndex("port")));
Log.d(TAG, "apnInfo=" + apnInfo);
list.add(apnInfo);
}
if(cr!=null){
cr.close();
}
Log.d(TAG, "getApnList---list" + list);
return list;
}
没有什么可讲的,就是数据库的相关操作。
网友评论