public final int REQUEST_CODE = 300;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//registerObserver();
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_SMS)
== PackageManager.PERMISSION_GRANTED&&ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_CALL_LOG)
== PackageManager.PERMISSION_GRANTED) {
getSmsFromPhone();//READ_CALL_LOG
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_SMS,Manifest.permission.READ_CALL_LOG}, REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED&&grantResults[1] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
getSmsFromPhone();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
}
}
private Uri SMS_INBOX = Uri.parse("content://sms/");
public void getSmsFromPhone() {
ContentResolver cr = getContentResolver();
//String[] projection = new String[] { "body" };//"_id", "address", "person",, "date", "type
//String where = " address = '1066321332' AND date > "
// + (System.currentTimeMillis() - 10 * 60 * 1000);
String[] projection = new String[] { "_id", "address", "person",
"body", "date", "type","read" };
Cursor cur = cr.query(SMS_INBOX, projection, null, null, "date desc");
if (null == cur)
return;
List<Integer> list = new ArrayList<>();
//List<String> list = new ArrayList<>();
if (cur.moveToNext()) {
String number = cur.getString(cur.getColumnIndex("address"));//手机号
String name = cur.getString(cur.getColumnIndex("person"));//联系人姓名列表
String body = cur.getString(cur.getColumnIndex("body"));
System.out.println("number="+number);
System.out.println("name="+name);
System.out.println("body="+body);
int isRead = cur.getInt(cur.getColumnIndex("read"));
System.out.println("isRead="+isRead);
if(isRead==0){ // 0 未读 1 已读
list.add(isRead);
}
//这里我是要获取自己短信服务号码中的验证码~~
Pattern pattern = Pattern.compile(" [a-zA-Z0-9]{10}");
Matcher matcher = pattern.matcher(body);
if (matcher.find()) {
String res = matcher.group().substring(1, 11);
//mobileText.setText(res);
}
} else {
System.out.println("no data");
}
System.out.println("未读消息数:"+list.size());
readMissCall();
}
private int readMissCall() {
int result = 0;
// Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
// new String[] { Calls.TYPE }, " type=? and new=?",
// new String[] { Calls.MISSED_TYPE + "", "1" }, "date desc");
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
new String[] { CallLog.Calls.TYPE }, " type=?",
new String[] { CallLog.Calls.MISSED_TYPE + ""}, "date desc");
if (cursor != null) {
result = cursor.getCount();
cursor.close();
}
System.out.println("未接来电数:"+result);
return result;
}
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
01-29 11:34:04.562 21721 21721 I System.out: number=100863114991285
01-29 11:34:04.562 21721 21721 I System.out: name=0
01-29 11:34:04.562 21721 21721 I System.out: body=您好,截止到01月29日11时25分,您定制套餐中剩余流量总计12420.55MB,剩余语音0分钟,您的账户余额为20.94元。【查询更多套餐余量等信息点此链接http://dx.10086.cn/wxgrzy
01-29 11:34:04.562 21721 21721 I System.out: 】提示:只要有剩余通用流量、通用语音分钟数,就不会收取额外费用,如需详情和其他服务请回复如下代码:
01-29 11:34:04.562 21721 21721 I System.out: 1 余额明细
01-29 11:34:04.562 21721 21721 I System.out: 2 流量使用详情
01-29 11:34:04.562 21721 21721 I System.out: 3 语音使用详情
01-29 11:34:04.562 21721 21721 I System.out: 4 开通30元1024MB可选包
01-29 11:34:04.562 21721 21721 I System.out: 5 开通流量安心包
01-29 11:34:04.562 21721 21721 I System.out: 6 更换流量套餐
01-29 11:34:04.562 21721 21721 I System.out: 7 抽奖赢话费和流量!【中国移动】
01-29 11:34:04.562 21721 21721 I System.out: isRead=0
01-29 11:34:04.563 21721 21721 I System.out: 未读消息数:1
01-29 11:34:04.602 21721 21721 I System.out: 未接来电数:32
未读消息未接来电数5
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...