Android開發之電話服務、SMS簡訊監聽
阿新 • • 發佈:2019-02-16
電話服務
//檢測裝置是否有電話硬體
boolean telephonySupported = pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
//獲得電話型別
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String typeStr = null;
int phoneType = telephonyManager.getPhoneType();
switch (phoneType) {
case TelephonyManager.PHONE_TYPE_CDMA:
typeStr = "CDMA";
break;
case TelephonyManager.PHONE_TYPE_GSM:
typeStr = "GSM";
break;
case TelephonyManager.PHONE_TYPE_SIP:
typeStr = "SIP";
break ;
case TelephonyManager.PHONE_TYPE_NONE:
typeStr = "None";
break;
}
//需要許可權如下
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
TelephonyManager telephonyManager = (TelephonyManager)
getSystemService(Context. TELEPHONY_SERVICE);
//獲得連線網路所在國家的iso程式碼,cn
String country = telephonyManager.getNetworkCountryIso();
//獲得連線網路的運營商ID,46000
String id = telephonyManager.getNetworkOperator();
//獲得連線網路的運營商名稱,中國移動
String name = telephonyManager.getNetworkOperatorName();
Log.i("123123", "country:" + country + "," + "id:" + id
+ "," + "name:" + name);
//獲取所連線網路的型別
TelephonyManager telephonyManager = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
int netType = telephonyManager.getNetworkType();
switch (netType) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
Log.i("123123","1xRTT" );
break;
case TelephonyManager.NETWORK_TYPE_LTE:
Log.i("123123","LTE" );
break;
}
//許可權
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
//獲取sim詳細資訊
TelephonyManager telephonyManager = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
int simState = telephonyManager.getSimState();
switch (simState) {
case TelephonyManager.SIM_STATE_READY:
//國家程式碼
String country = telephonyManager.getSimCountryIso();
//運營商程式碼
String operatorCode = telephonyManager.getSimOperator();
//運營商的名稱
String operatorName = telephonyManager.getSimOperatorName();
//序列號
String simSerial = telephonyManager.getSimSerialNumber();
break;
}
//讀取移動資料的狀態
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int state = telephonyManager.getDataState();
switch (state) {
case TelephonyManager.DATA_CONNECTED:
Log.i("123123", "已連線");
break;
case TelephonyManager.DATA_CONNECTING:
Log.i("123123", "正在連線");
break;
case TelephonyManager.DATA_DISCONNECTED:
Log.i("123123", "沒有連線");
break;
case TelephonyManager.DATA_SUSPENDED:
Log.i("123123", "暫停");
break;
}
SMS簡訊監聽
監聽傳入的SMS訊息。建立廣播接收器,設定action。新增所需要的許可權。
<receiver
android:name=".sms.MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
//Android 6.0及以上需要執行時許可權
<uses-permission android:name="android.permission.RECEIVE_SMS" />
重寫廣播接收器的onReceive方法。
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
//獲取pdus陣列
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
//對SmsMessage陣列賦值
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
for (SmsMessage smsMessage : messages) {
//簡訊的內容
String msg = smsMessage.getMessageBody();
//簡訊的接收時間
long when = smsMessage.getTimestampMillis();
//簡訊傳送方號碼
String from = smsMessage.getOriginatingAddress();
Date date = new Date(when);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String ss = simpleDateFormat.format(date);
Log.i("123123", msg + "," + ss + "," + from);
}
}
}