我用broadcastreceiver注册生成了个广播接收器,准备拦截短信信息的,action是这个<action android:name="android.provider.TelePhony.SMS_RECEIVED"/>
当有新短信来的时候,可以执行onReceiver里面的代码,但是onReceiver里面调用的abortBroadcast();
也就是要终止广播,不给系统接受,但是貌似么用,系统照样提示有新短信
manifest中权限<uses-permission android:name="android.permission.RECEIVE_SMS" />也给了
广播接收器的优先级priority也设置为1000,听说一些设置为2147483647也试过,都不行,
楼主自学小白一个求各位大腿解答~
4.3+这方法就没暖用了 淡定
没有别的办法了么?在网上能搜出来的拦截短信功能的都是用abortBroadcast()。。。郁闷啊
到处百度找到了这个方法,创建个项目继承ContentObserver(内容观察者),监听收件箱的数据变化,有新信息来就把信息给获取到,代码如下
public class SMSContentObserver extends ContentObserver{
private Context mContext ;
private Handler mHandler ;
private int a = 0;
public SMSContentObserver(Context context,Handler handler) {
super(handler);
mContext = context ;
mHandler = handler ;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Cursor cursor = mContext.getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
ContentValues contentValues = new ContentValues();
if (cursor.getCount() > 0) {
cursor.moveToFirst();
contentValues.put("person", cursor.getString(cursor.getColumnIndex("person")));
contentValues.put("address", cursor.getString(cursor.getColumnIndex("address")));
contentValues.put("body", cursor.getString(cursor.getColumnIndex("body")));
contentValues.put("date", cursor.getString(cursor.getColumnIndex("date")));
contentValues.put("type", cursor.getString(cursor.getColumnIndex("type")));
}
cursor.close();
mHandler.obtainMessage(a, contentValues).sendToTarget();
}
}
在Activity里面注册个 ContentObserver
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SMSContentObserver smsContentObserver = new SMSContentObserver(this, mHandler);
getContentResolver().registerContentObserver(Uri.parse("content://sms/inbox"), true, smsContentObserver);}
//通过Handler传递数据
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
ContentValues cv = (ContentValues) msg.obj;
database = new Database(MainActivity.this);
dbRead = database.getReadableDatabase();
cursorRead = dbRead.query("MyMessage", null,"body=? and date=?", new String[]{(String) cv.get("body"), (String) cv.get("date")}, null, null, null);
if (cursorRead.getCount() == 0) {
database = new Database(MainActivity.this); //这里是自己创建的一个数据库,把监听到的短信保存到这数据库
dbWrite = database.getWritableDatabase(); //
dbWrite.insert("MyMessage", null, cv); //
etNumber.setText((String) cv.get("address"));
getContentResolver().delete(Uri.parse("content://sms/inbox"), "body=? and date=?", String[]{(String) cv.get("body"), (String) cv.get("date")});//这里把那条数据给删除了,起到截获短信效果
refreshListView();
}
}
}
};
新手小白,有什么不对的地方就说下,请多多包涵。