2

我正在尝试创建 android (Froyo 2.2) SMS 日志记录应用程序。使用许多教程,我制作了应用程序的主要部分,它就像一个魅力!基本上它读取最后一条短信(发送或接收),通过电话号码获取联系人姓名,将毫秒 unixtime 转换为日期并在屏幕上显示结果。

现在我想删除 GUI,修改该代码,以便该应用程序将监视手机是否有任何传入/传出的短信活动,如果有,它将启动提到的代码并将结果保存到文件中。

我希望整个应用程序对用户完全透明/隐身(在后台工作)。我怀疑我必须以某种方式使用 ContentObserver 类,但我在实现方面遇到了一些问题(即使开始)。

你能帮忙吗?

当前代码:

import android.app.Activity;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.widget.TextView;
import android.net.Uri;
import android.database.Cursor;
import java.util.Date;

public class SMSLog extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);

        Uri uriSMS = Uri.parse("content://sms/");
        Cursor cur = getContentResolver().query(uriSMS, null, null, null, null);
        cur.moveToNext();

        String body = cur.getString(cur.getColumnIndex("body"));   
        String add = cur.getString(cur.getColumnIndex("address"));
        String time = cur.getString(cur.getColumnIndex("date"));
        String protocol = cur.getString(cur.getColumnIndex("protocol"));       

        String contactName = "";
        Uri personUri = Uri.withAppendedPath( ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(add));  
        Cursor c = getContentResolver().query(personUri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null );  
        if( c.moveToFirst() ) {  
            int nameIndex = c.getColumnIndex(PhoneLookup.DISPLAY_NAME);  
            contactName = c.getString(nameIndex); 
        }
        c.close();
        cur.close();

        String out = "";
        Date d = new Date(Long.valueOf(time)); 

        if (protocol == null)
            out = "Sending to: "+ contactName + " <"+add +">\nDate: "+d +"\nBody: "+body+"\n\n";
        else 
            out = "Received from: "+ contactName + " <"+add +">\nDate: "+d +"\nBody: "+body+"\n\n";

        tv.setText(out);
        setContentView(tv);

    }
}
4

0 回答 0