584425439@qq.com
584425439@qq.com
  • 发布:2019-01-11 08:53
  • 更新:2019-01-11 09:22
  • 阅读:719

监听状态栏通知代码,android

分类:Native.js
new Vue({  
        el: '#app',  
        data(){  
            return{  
                auth: false,  
                listening: false,  
                main:'',  
                msg: {},  
                callBackUrl:''  

            }  
        },  
        created() {  
            // 初始化  
            this.init()  
        },  
        methods: {  
            init(){  
                var that = this  
                document.addEventListener('plusready', function(){  
                    //  导入android类对象  
                    var Intent = plus.android.importClass("android.content.Intent");  
                    var context = plus.android.importClass("android.content.Context");//获取上下文  
                    var ComponentName = plus.android.importClass("android.content.ComponentName");  
                    var PackageManager = plus.android.importClass("android.content.pm.PackageManager");  
                    var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");  
                    var IntentFilter = plus.android.importClass('android.content.IntentFilter'); //引入过滤器  
                    var Intent = plus.android.importClass('android.content.Intent');  
                    var ActivityManager = plus.android.importClass("android.app.ActivityManager");  
                    plus.android.importClass("java.util.List");  
                    plus.android.importClass("java.util.Set");  
                    // 获取主Activity对象的实例  
                    that.main = plus.android.runtimeMainActivity();  
                    var filter = new IntentFilter();  
                    var intent = new Intent();  
                    //启动短信监听  
                    intent.setClassName(that.main, "com.cnupai.listen.server.WeChatNotificationListenerService");  
                    that.main.startService(intent);   

                    //检测通知权限是否打开  
                    function isNotificationListenerServiceEnabled(){  
                        var packageNames = NotificationManagerCompat.getEnabledListenerPackages(that.main);  
                        if (packageNames.contains(that.main.getPackageName())) {  
                            console.log(1);  
                            that.auth = true;  
                        }else{  
                            console.log(0);  
                            that.auth = false;  
                            var Intent = plus.android.importClass('android.content.Intent');  
                            that.main.startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));  
                        }  
                    };  
                    //重新绑定 reBind  
                    var thisComponent = new ComponentName(that.main, "com.cnupai.listen.server.WeChatNotificationListenerService" )  
                    var pm = that.main.getPackageManager();  
                    pm.setComponentEnabledSetting(thisComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);  
                    pm.setComponentEnabledSetting(thisComponent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);  

                    var receiver = plus.android.implements("io.dcloud.feature.internal.reflect.BroadcastReceiver",{  
                        onReceive:function(context,intent){  
                            plus.android.importClass(intent);  
                            //console.log(intent.getExtra("notificationInfo"));  
                            that.msg = JSON.parse(intent.getExtra("notificationInfo"));  
                            axios.post(that.callBackUrl, that.msg).then(res=>{  
                                console.log(res);  
                            }).catch(e=>{  
                                console.log(e);  
                            });  
                            //that.msgList =  [JSON.parse(intent.getExtra("notificationInfo"))].concat(that.msgList);  
                        }  
                    });   
                    filter.addAction('location.reportsucc') //监听扫码广播  
                    that.main.registerReceiver(receiver, filter); //注册监听        
                    //检测是否在运行  
                    function isServiceRunning(){  
                        var activityManager=that.main.getSystemService(context.ACTIVITY_SERVICE);  
                        that.listening = plus.android.invoke('com.cnupai.listen.server.Util', 'isServiceRunning', activityManager, 'com.cnupai.listen.server.WeChatNotificationListenerService');  
                        if(that.listening){  
                            console.log('监听中');  
                        }else{  
                            console.log('未监听,重新绑定');  
                            //that.main.startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));  
                            intent.setClassName(that.main, "com.cnupai.listen.server.WeChatNotificationListenerService");  
                            /** 退出Activity是,停止服务 */  
                            //main.stopService(intent);  
                            that.main.startService(intent);   
                        }  
                    };  
                    setInterval(isServiceRunning, 3000);  
                    setInterval(isNotificationListenerServiceEnabled, 3000);  
                });  
            },  
            openAuth(){  
                var Intent = plus.android.importClass('android.content.Intent');  
                this.main.startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));  
            }  
        }  
    })  
0 关注 分享

要回复文章请先登录注册

584425439@qq.com

584425439@qq.com (作者)

体验链接,http://212.64.83.121:89/upload/监听状态栏,需要的联系584425439.apk
2019-01-11 09:22
584425439@qq.com

584425439@qq.com (作者)

定制app请找我,584425439@qq.com
2019-01-11 08:57