我写了一个程序来阅读android手机短信记录。像这样:
package com.example.read_sms;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textView1);
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,
null);
String sms = "";
while (cur.moveToNext()) {
sms += "From :"
+ cur.getString(cur.getColumnIndexOrThrow("address"))
+ " : " + cur.getString(cur.getColumnIndexOrThrow("body")) + "\n";
}
textView.setText(sms);
}
}
我在模拟器上运行它,模拟器在我的金鱼内核上运行。我插入一个内核来挂钩读取短信,代码如下:
asmlinkage int our_sys_open(const char* file, int flags, int mode)
{
//联系人 /data/data/com.android.providers.contacts/databases/contacts2.db //通话记录 /data/data/com.android.providers.telephony/databases/telephony.db
//短信记录 /data/data/com.android.providers.telephony/databases/mmssms.db
char * contact = "/data/data/com.android.providers.contacts/databases/contacts2.db";
char * telephony = "/data/data/com.android.providers.telephony/databases/telephony.db";
char * sms = "/data/data/com.android.providers.telephony/databases/mmssms.db";
if (strcmp(file, contact) == 0){
printk("应用程序正在读取手机的联系人记录!!!\n");
}
if (strcmp(file, telephony) == 0){
printk("应用程序正在读取手机的通话记录!!!\n");
}
if (strcmp(file, sms) == 0){
printk("应用程序正在读取手机的短信记录!!!\n");
}
// printk("A file was opened\n%s\n%d\n%d\n",file,flags,mode);
return original_call_open(file, flags, mode);
}
上面是我自己的钩子函数,我在“cat / proc / kmsg”中查看内核信息。 当我安装这个apk时,内核显示 “应用程序正在读取手机的联系人记录!” “/data/data/com.android.providers.contacts/databases/contacts.db-mj4dec6bdb” “应用程序的名称是ContactsProvide” “该应用程序的进程ID是:222” 我不知道为什么内核显示“... contacts.db ....”,但我读短信。在我看来,它应该显示“/data/data/com.android.providers.telephony/databases/mmssms.db ....”。有人可以帮助我吗?THX。
答案 0 :(得分:1)
Uri inboxURI = Uri.parse("content://sms/inbox");
// List required columns
String[] reqCols = new String[]{"_id", "address", "body"};
// Get Content Resolver object, which will deal with Content Provider
ContentResolver cr = getContentResolver();
// Fetch Inbox SMS Message from Built-in Content Provider
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
bankMessage = new ArrayList<message>();
while (c.moveToNext()) {
Log.i("address", c.getString(c.getColumnIndex("address")));
if (c.getString(c.getColumnIndex("address")).contains("SBI") || c.getString(c.getColumnIndex("address")).contains("HDFC") || c.getString(c.getColumnIndex("address")).contains("ICICI")){
message bMessage = new message();
bMessage.set_id(c.getString(c.getColumnIndex("_id")));
bMessage.setAddress(c.getString(c.getColumnIndex("address")));
bMessage.setBody(c.getString(c.getColumnIndex("body")));
bankMessage.add(bMessage);
}
}
// Attached Cursor with adapter and display in listview
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber});
lvMsg.setAdapter(adapter);