检查是否是系统默认的短信代码如下:
public static boolean isDefaultSmsApp(Context myContext) {
try {
if (myContext == null) {
myContext = App.mContext;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
String packageName = Telephony.Sms.getDefaultSmsPackage(myContext);
if (Util.isNullOrEmpty(packageName)) {
return false;
}
if (packageName.equals(myContext.getPackageName())) {
return true;
}
return false;
} else {
return true;
}
}catch (Exception e){
e.printStackTrace();
Util.upToErrorInfo(App.mContext , e);
}
return false;
}
替换系统默认的短信方法如下:
public static void goToSettingDefaultMsg(Context mContext) {
try{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
RoleManager roleManager = (RoleManager)
mContext.getSystemService(Context.ROLE_SERVICE);
Intent mIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS);
mContext.startActivityForResult(mIntent , requestCode);
}else{
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
mContext.getPackageName());
CommonEventUtils.replaceDefaultMsgEvent(mContext);
mContext.startActivity(intent);
}
}catch(Exception e){
e.printStackTrace();
Util.upToErrorInfo(App.mContext, e);
}
}
主要通过学习了https://my.oschina.net/zhuzihasablog/blog/140344来实现获取短信内容,需要添加权限并检查权限
<uses-permission android:name="android.permission.READ_SMS" />
如果要实现短信拦截和插入等功能,需要实现将app替换成系统默认的短信,接下来步入正题,试下获取短信会话表的内容,在获取会话表之前需要获取content://mms-sms/canonical-addresses表中的address和_id(该_id为会话表中的recipient_ids),用来匹配电话号码。获取content://mms-sms/canonical-addresses表的代码如下
String[] projection = new String[]{"address", "_id"};
Uri mUri = Uri.parse("content://mms-sms/canonical-addresses");
Cursor mCursor = null;
try {
mCursor = getActivity().getContentResolver().query(mUri , projection ,
null , null , null);
if(mCursor != null){
while (mCursor.moveToNext()){
try {
String address = mCursor.getString(mCursor.getColumnIndex("address"));//电话号码
String _id = mCursor.getString(mCursor.getColumnIndex("_id"));//id
}
}catch (Exception e){
e.printStackTrace();
Util.upToErrorInfo(App.mContext , e);
}
}
}else{
LogUtil.e(TAG , "cursor == null");
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(mCursor != null){
mCursor.close();
}
}
接下来就是实现获取短信会话表中的所有内容,获取短信会话表需要从content://mms-sms/conversations?simple=true中获取,代码如下:
Uri mUri = Uri.parse("content://mms-sms/conversations?simple=true");
String[] projection = new String[]{"message_count", "recipient_ids", "snippet","snippet_cs",
"read", "date", "_id"};
Cursor cursor = null;
try {
cursor = mContext.getContentResolver().query(mUri , projection ,
"date < " + System.currentTimeMillis(), null, "date desc");
if(cursor != null){
while (cursor.moveToNext()){
String message_count = cursor.getString(cursor.getColumnIndex("message_count"));//获取短信的个数
String recipient_ids = cursor.getString(cursor.getColumnIndex("recipient_ids"));//通过此id获取电话号码
String snippet = cursor.getString(cursor.getColumnIndex("snippet"));//获取短信的内容
String snippet_cs = cursor.getString(cursor.getColumnIndex("snippet_cs"));//通过这个判断是彩信还是短信
int read = cursor.getInt(cursor.getColumnIndex("read"));//判断此条信息阅读状态
String date = cursor.getString(cursor.getColumnIndex("date"));//短信的时间
String _id = cursor.getString(cursor.getColumnIndex("_id"));//短信会话表的_id,用来获取短信详情
if(message_count == null || message_count.equals("0")){
continue;
}
if(recipient_ids.contains(" ")){//如何recipient_ids含有空格的话,就可能是群发短信
String[] threadIDs = recipient_ids.split(" ");
int len = threadIDs.length;
for(int i = 0 ; i < len ; i ++){
//TODO 这里可以更具 threadIDs[i] 和上一步从content://mms-sms/canonical-addresses表中的_id来匹配电话号码
}
}else{
//TODO 这里可以更具 threadIDs[i] 和上一步从content://mms-sms/canonical-addresses表中的_id来匹配电话号码
}
if(!TextUtils.isEmpty(snippet_cs) && snippet_cs.equals("0")){//这里snippet表示是短信的内容
if(!TextUtils.isEmpty(snippet)){//如果snippet为空的话
snippet = snippet.replace("\n" , "");
}else{
snippet = getMsgBodyByID(mContext , recipient_ids);//通过recipient_id 获取短信内容
}
}else{//这里是彩信
if(!TextUtils.isEmpty(snippet)){//这里snippet表示是彩信的内容了
try {
//这里需要转码,有些手机不需要,具体视情况而定
snippet = new String(snippet.getBytes("ISO8859_1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}else {
snippet = "";
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(cursor != null){
cursor.close();
}
}
private static String getMsgBodyByID(Context mContext , String ID){
String body = "" ;
if(TextUtils.isEmpty(ID)){
return body;
}
try {
Uri SMS_INBOX = Uri.parse("content://sms");
ContentResolver cr = mContext.getContentResolver();
String selection = "thread_id = ?";
String[] selectionArgs = {String.valueOf(ID)};
Cursor mCursor = null;
try {
mCursor = cr.query(SMS_INBOX, null, selection, selectionArgs, "date desc");
if (mCursor != null && mCursor.getCount() > 0) {
if (mCursor.moveToNext()) {
body = mCursor.getString(mCursor.getColumnIndex("body"));
}
}else{
List<ModelMessage> mList = getMessageByIDMMS(mContext , ID);
if(mList.size() > 0){
body = "彩信 : " ;//这里后面会在获取短信详情中进行处理
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(mCursor != null){
mCursor.close();
}
}
}catch (Exception e){
e.printStackTrace();
}
return body;
}