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"));
}
}
})
5***@qq.com
- 发布:2019-01-11 08:53
- 更新:2021-09-29 09:41
- 阅读:3754
8 个评论
要回复文章请先登录或注册
9***@qq.com
2***@qq.com
Sean_2020
小雄Milo
狗二万
1***@qq.com
5***@qq.com (作者)
5***@qq.com (作者)