情诗网 >情话短信 > 正文

短信、来电、微信、QQ消息监听

来源:情诗网    2020-12-07    分类:情话短信

上一篇获取微信qq等三方通知(http://www.jianshu.com/p/eba0a679cd31),说了如何使用NotificationListenerService,以及操作过程中的几个大坑,现在利用NotificationListenerService实现短信、来电、微信、QQ消息监听。

1.还是先来一个类继承NotificationListenerService.
如下代码中,含一个参数的onNotificationPosted和onNotificationRemoved两个方法,每次来通知发现无响应,而含两个参数的onNotificationPosted和onNotificationRemoved就有响应。post是通知来时调用,removed是通知删除时调用。

public class NotifyService extends NotificationListenerService {

    public static final String SEND_WX_BROADCAST="SEND_WX_BROADCAST";
    
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
    }
    
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
        Log.e("AAA", "=2==onNotificationPosted   ID :"
                + sbn.getId() + "\t"
                + sbn.getNotification().tickerText + "\t"
                + sbn.getPackageName());
        String packageName=sbn.getPackageName();
        Intent intent=new Intent();
        intent.setAction(SEND_WX_BROADCAST);
        Bundle bundle=new Bundle();
        bundle.putString("packageName",packageName);
        intent.putExtras(bundle);
        this.sendBroadcast(intent);

    }
    
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
        Log.e("AAA", "=4==onNotificationRemoved   ID :"
                + sbn.getId() + "\t"
                + sbn.getNotification().tickerText
                + "\t" + sbn.getPackageName());
    }
}

2.当然要在清单文件中注册这个服务

<service android:name=".NotifyService"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
    <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
</service>

3.使用广播把监听的包名传到需要的地方
因为NotificationListenerService这个服务提供了一个系统的对象,我们new出来的对象无法使用,所以我放弃使用接口回调,采用广播的方式传递包名。代码如上。

4.一个接口,在接收广播的地方,进行回调。

public interface MyMessage {

    /**
     * 电话来了
     */
    public void comePhone();

    /**
     * 短信来了
     */
    void  comeShortMessage();

    /**
     * 微信消息
     */
    void  comeWxMessage();

    /**
     * qq消息
     */
    void comeQQmessage();
}

5.一个类用于接收广播以及判断是否打开设置

public class ComeWxMessage  {

    private static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners";
    public static final String SEND_WX_BROADCAST="SEND_WX_BROADCAST";
    public static final String QQ="com.tencent.mobileqq";
    public static final String WX="com.tencent.mm";
    public static final String MMS="com.android.mms";
    public static final String CALL="com.android.incallui";
    private MyMessage myMessage;
    private Context context;

    public ComeWxMessage(MyMessage myMessage, Context context) {
        this.myMessage = myMessage;
        this.context = context;
        registBroadCast();
    }

    private BroadcastReceiver b=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle=intent.getExtras();
            String pachageName=bundle.getString("packageName");
            switch (pachageName){
                case WX:
                    myMessage.comeWxMessage();
                    break;
                case QQ:
                    myMessage.comeQQmessage();
                    break;
                case MMS:
                    myMessage.comeShortMessage();
                    break;
                case CALL:
                    myMessage.comePhone();
                    break;
            }
        }
    };
    private void registBroadCast() {
        IntentFilter filter=new IntentFilter(SEND_WX_BROADCAST);
        context.registerReceiver(b,filter);
    }
    public void unRegistBroadcast(){
        context.unregisterReceiver(b);
    }
public void openSetting(){
        if (!isEnabled()) {
            Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            context.startActivity(intent);
        } else {
            Toast.makeText(context, "已开启服务权限", Toast.LENGTH_LONG).show();
        }
    }

    private boolean isEnabled() {
        String pkgName = context.getPackageName();
        final String flat = Settings.Secure.getString(context.getContentResolver(),
                ENABLED_NOTIFICATION_LISTENERS);
        if (!TextUtils.isEmpty(flat)) {
            final String[] names = flat.split(":");
            for (int i = 0; i < names.length; i++) {
                final ComponentName cn = ComponentName.unflattenFromString(names[i]);
                if (cn != null) {
                    if (TextUtils.equals(pkgName, cn.getPackageName())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public void toggleNotificationListenerService() {
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(
                new ComponentName(context,com.mytestnotify.NotifyService.class),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

        pm.setComponentEnabledSetting(
                new ComponentName(context,  com.mytestnotify.NotifyService.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }
}

6.在MainActivity里面调用。当然不需要上面这个类,可以直接在MainActivity里面接收广播,但感觉太乱了。

public class MainActivity extends AppCompatActivity implements MyMessage{
    
    private ComeWxMessage comeWxMessage;
    private MyMessage myMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myMessage=new MainActivity();

        comeWxMessage=new ComeWxMessage(myMessage,this);
        comeWxMessage.toggleNotificationListenerService();
        comeWxMessage.openSetting();
    }
    
    @Override
    public void comePhone() {
        Log.e("AAA","====回调中,收到来电===");
    }

    @Override
    public void comeShortMessage() {
        Log.e("AAA","====回调中,收到短信消息===");
    }

    @Override
    public void comeWxMessage() {
        Log.e("AAA","====回调中,收到微信消息===");
    }

    @Override
    public void comeQQmessage() {
        Log.e("AAA","====回调中,收到QQ消息===");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        comeWxMessage.unRegistBroadcast();
    }
}

至此,就可以实现了。
本来对于短信和来电,我用另外的方法进行监听,但在监听微信的时候,幸得别人提醒使用NotificationListenerService。在进行测试的时候,发现来电、短信都可以通过通知栏

热门文章