短信窃听器

短信窃听器

 


添加读写的权限
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.WRITE_SMS"/>

package cn.itcast.smslistener;

import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;

public class DemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Uri uri = Uri.parse("content://sms/");
        getContentResolver().registerContentObserver(uri, true, new MyObserver(new Handler()));
    }
   
   
    private class MyObserver extends ContentObserver{

                public MyObserver(Handler handler) {
                        super(handler);
                        // TODO Auto-generated constructor stub
                }

                @Override
                public void onChange(boolean selfChange) {
                        super.onChange(selfChange);
                        System.out.println("短信内容发生改变啦");
                        Cursor cursor = getContentResolver().query(Uri.parse("content://sms/"), null, null, null, "date desc");
                        if(cursor.moveToFirst()){
                                String body = cursor.getString( cursor.getColumnIndex("body"));
                                System.out.println(body);
                        }
                }
    }
}

本文出自 “蒲公英的梦想” 博客,转载请与作者联系!

你可能感兴趣的:(短信窃听器)