[android] 手机卫士黑名单功能(短信拦截)

2019-09-10 15:51:28 浏览数 (1)

前面我们把需要拦截的手机号都存储和展示出来了,接下来是使用广播接收者拦截短信了,这个广播接收者需要和一个服务绑定,服务开启的时候,接收者存在,服务停掉时,接收者关闭

在service包下定义一个类CallSmsSafeService继承系统的Service

重写onCreate()方法

获取BroadcastReceiver对象,new出来

调用registerReceiver()方法,注册广播,参数:BroadcastReceiver对象,IntentFillter对象

重写onDestory()方法

调用unregisterReceiver()方法,参数:BroadcastReceiver对象

BroadcastReceiver对象置为null

定义一个内部类InnerSmsReceiver继承系统的BroadcastReceiver

重写 onReceive()方法,传递进来参数:Context对象,Intent对象

调用Intent对象的getExtras().get(“pdus”)方法,获取到Object[]数组

for循环Object[]数组,每个是Object对象

调用SmsMessage.createFromPdu()方法,获取到SmsMessage对象,参数:byte[]数组,强转Object对象

调用SmsMessage对象的getOriginatingAddress()方法,得到String发件人

调用Dao对象的查询数据库方法,查出这条短信的拦截模式

判断拦截模式,如果是全部拦截3或者短信拦截2,就调用abortBroadcast()方法

在设置中心对上面的服务进行配置,达到开启和关闭的功能

查看这篇:http://www.cnblogs.com/taoshihan/p/5468523.html

CallSmsSafeService.java

代码语言:javascript复制
package com.qingguow.mobilesafe.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.telephony.SmsMessage;

import com.qingguow.mobilesafe.db.ado.BlackNumberAdo;
/**
 * 短息电话拦截服务
 * @author taoshihan
 *
 */
public class CallSmsSafeService extends Service {
    private InnerSmsReceiver receiver;
    private BlackNumberAdo ado;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    /**
     * 服务创建
     */
    @Override
    public void onCreate() {
        receiver=new InnerSmsReceiver();
        IntentFilter filter=new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        ado=new BlackNumberAdo(getApplicationContext());
        registerReceiver(receiver, filter);
        super.onCreate();
    }
    /**
     * 接收广播
     * @author taoshihan
     *
     */
    private class InnerSmsReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Object[] objs=(Object[]) intent.getExtras().get("pdus");
            for(Object obj:objs){
                SmsMessage sms=SmsMessage.createFromPdu((byte[]) obj);
                String sender=sms.getOriginatingAddress();
                
                String mode=ado.find(sender);
                if(mode!=null){
                    if (mode.equals("2") || mode.equals("3")) {
                        System.out.println("拦截短信:"   sender);
                        abortBroadcast();
                    }
                }
                
            }
        }
        
    }
    @Override
    public void onDestroy() {
        unregisterReceiver(receiver);
        receiver=null;
        super.onDestroy();
    }
}

ado类

代码语言:javascript复制
package com.qingguow.mobilesafe.db.ado;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.qingguow.mobilesafe.db.BlackNumberDBOpenHelper;

public class BlackNumberAdo {
    private BlackNumberDBOpenHelper helper;

    public BlackNumberAdo(Context context) {
        helper = new BlackNumberDBOpenHelper(context);
    }

    /**
     * 插入数据
     * 
     * @param phone
     * @param mode
     */
    public void add(String phone, String mode) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("phone", phone);
        values.put("mode", mode);
        db.insert("blacknumber", null, values);
        db.close();
    }

    /**
     * 查询全部
     * 
     * @return
     */
    public List<Map<String, String>> findAll() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery(
                "select phone,mode from blacknumber order by id desc", null);
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        while (cursor.moveToNext()) {
            Map<String, String> info = new HashMap<String, String>();
            String phone = cursor.getString(0);
            String mode = cursor.getString(1);
            info.put("phone", phone);
            info.put("mode", mode);
            list.add(info);
        }
        cursor.close();
        db.close();
        return list;
    }

    /**
     * 修改数据
     * 
     * @param phone
     * @param mode
     */
    public void update(String phone, String mode) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("phone", phone);
        values.put("mode", mode);
        db.update("blacknumber", values, "phone=?", new String[] { phone });
        db.close();
    }

    /**
     * 删除数据
     * 
     * @param phone
     */
    public void delete(String phone) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.delete("blacknumber", "phone=?", new String[] { phone });
        db.close();
    }

    /**
     * 查找单条
     * 
     * @param phone
     */
    public String find(String phone) {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery(
                "select mode from blacknumber where phone=?",
                new String[] { phone });
        if (cursor.moveToNext()) {
            String mode = cursor.getString(0);
            return mode;
        }

        return null;
    }
}

0 人点赞