如何在android中获取短信登录
Posted
技术标签:
【中文标题】如何在android中获取短信登录【英文标题】:How to get SMS log in android 【发布时间】:2013-03-24 17:04:37 【问题描述】:我在 android 中使用 csv 文件导出日志。我只得到通话日志但没有得到短信日志。这里是代码
while (curLog.moveToNext())
String callName = curLog
.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
if (callName == null)
callName="Unknown";
String callNumber = curLog.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
String callDate = curLog.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.DATE));
String callType = curLog.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.TYPE));
String duration = curLog.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.DURATION));
data.add(new String[] callName,callNumber,callDate,callType,duration);
请建议如何解决它.....
【问题讨论】:
检查这两个答案:[one][1] 和 [two][2] [1]: ***.com/a/15638148/1168654 [2]: ***.com/a/15761291/1168654 @DhavalSodhaParmar 我不看短信我只需要获取日志 我的两台三星设备都有同样的问题。研究这个问题表明三星处理android日志的方式存在问题,它将它们合并。显然,这是不正确且不一致的行为。注意,短信不能通过通话记录API删除,但可以通过通话记录API检索。 ***.com/q/13161461/1168654 欲了解更多详情,请查看此链接,他们使用两种不同的方法获取呼叫和短信计数:***.com/questions/7621893/… @DhavalSodhaParmar 感谢您的贡献 【参考方案1】:这是我获取短信的方式。它可以获取当前消息的正文、内容、日期以及检查它是传入还是传出。另外,它可以通过其他方法获取与电话号码匹配的联系人姓名。
首先,给AndroidManifest.xml文件添加权限。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.app.myapp" >
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
...
</manifest>
其次,实现 SMS Observer 类来获取你需要的信息
public class SMSObserver extends ContentObserver
private Context context;
private static final String TAG = "SMSObserver";
private static final Uri SMS_URI = Telephony.Sms.CONTENT_URI; //get uri depending on different devices
public SMSObserver(Handler handler, Context context)
super(handler);
this.context = context;
@Override
public boolean deliverSelfNotifications()
return true;
//this onChange method will be fired up when you send or receive message
@Override
public void onChange(boolean selfChange)
Cursor cursor = context.getContentResolver().query(SMS_URI, null, null, null, Telephony.Sms.DATE + " DESC");
String phNumber = null;
//make sure there is a message being operating
if (cursor != null && cursor.moveToFirst())
try
//the meanings of those variables are quite straight forward
String type = cursor.getString(cursor.getColumnIndex(Telephony.Sms.TYPE));
String content = cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
String date = cursor.getString(cursor.getColumnIndex(Telephony.Sms.DATE));
Date SMSDate = new Date(Long.valueOf(date));
phNumber = cursor.getString(cursor.getColumnIndex(Telephony.Sms.ADDRESS)); //this is phone number rather than address
String contact = getContactDisplayNameByNumber(phNumber); //call the metod that convert phone number to contact name in your contacts
int typeCode = Integer.parseInt(type);
String direction = "";
//get the right direction
switch (typeCode)
case Telephony.Sms.MESSAGE_TYPE_INBOX:
direction = "INCOMING";
break;
case Telephony.Sms.MESSAGE_TYPE_OUTBOX:
direction = "OUTGOING";
break;
case Telephony.Sms.MESSAGE_TYPE_SENT:
direction = "SENT";
break;
default:
direction = "UNKNOWN";
Log.e(TAG, typeCode + " is unknown");
break;
catch (CursorIndexOutOfBoundsException e)
Log.e(TAG, "SMSHelper: outgoingSMS", e);
finally
cursor.close();
Toast.makeText(context, "\nName: " + contact + "\nPhone Number:--- " + phNumber + " \nContent:--- "
+ content + " \nCall Date:--- " + SMSDate
+ " Direction: " + direction, Toast.LENGTH_LONG).show();
Log.i(TAG, "\nName: " + contact + "\nPhone Number:--- " + phNumber + " \nContent:--- "
+ content + " \nCall Date:--- " + SMSDate
+ " Direction: " + direction + "\n----------------------------------");
super.onChange(selfChange);
/**
* http://***.com/questions/3712112/search-contact-by-phone-number
* Look up phone number
*
* @param number phone number
* @return the name matched with the phone number
*/
private String getContactDisplayNameByNumber(String number)
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String name = "<Not in contact list>";
ContentResolver contentResolver = context.getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[]BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, null, null, null);
try
if (contactLookup != null && contactLookup.getCount() > 0)
contactLookup.moveToFirst();
name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
//String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
Log.i(TAG, "Found number in contacts: " + number + " = " + name);
else
Log.e(TAG, "Cursor is null or empty " + number + " not found in contacts");
finally
if (contactLookup != null)
contactLookup.close();
return name;
如果设备运行的是 Android 6.0(API 级别 23)并且 app的目标SdkVersion为23或更高,短信权限为 被视为危险的许可。因此你必须得到正确的 事先许可。
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED
&&ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED)
//grant permission
ActivityCompat.requestPermissions( this,
new String[]Manifest.permission.READ_CONTACTS,Manifest.permission.READ_SMS,
10);
return;
最后,在 HomeActivity 类或任何其他类中实例化 SMSObserver 类 其他活动类。
//sms log
smsObserver = new SMSObserver(new Handler(), getApplicationContext());
getContentResolver().registerContentObserver(Telephony.Sms.CONTENT_URI, true, smsObserver);
【讨论】:
以上是关于如何在android中获取短信登录的主要内容,如果未能解决你的问题,请参考以下文章