设备 BOOT_COMPLETE 后 NotificationListenerService 不起作用 [英] NotificationListenerService not working after device BOOT_COMPLETE

查看:22
本文介绍了设备 BOOT_COMPLETE 后 NotificationListenerService 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NotificationListnerService 它在一段时间后工作正常它无法工作,特别是在设备启动后它没有启动我已经给了监听器所需的所有权限,这是我的代码.

I am working with NotificationListnerService it is working fine after some time it can not work and specially after device is boot it is not starting i have given all permissions which are required for listner here is my code.

public class Whatsapp_listner extends NotificationListenerService
{
 private static final String TAG = "NotificationListener";
 private static final String WA_PACKAGE = "com.whatsapp";

@Override
public void onListenerConnected() {
    super.onListenerConnected();
    Log.i("tag","Listner conneted");
}

@Override
public void onCreate() {
    super.onCreate();
}
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    if (!sbn.getPackageName().equals(WA_PACKAGE)) return;


    long yourmilliseconds = sbn.getPostTime();
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
    Date resultdate = new Date(yourmilliseconds);
    String date = sdf.format(resultdate);

    SimpleDateFormat my_time=new SimpleDateFormat("hh:mm aa");
    Date timer=new Date(yourmilliseconds);
    String time=my_time.format(timer);




    String modifiyedUniq = sbn.getUid() + sbn.getId() + sbn.getTag();


    Log.e(TAG, "Notification Key :: " + sbn.getKey());
    Log.e(TAG, "Notification Id :: " + sbn.getId());
    Log.e(TAG, "Notification postTime :: " + time);
    Log.e(TAG, "Notification From :: " + sbn.getPackageName());
    Log.e(TAG, "Notification TikerText :: " + sbn.getNotification().tickerText);
    Log.e(TAG, "Notification Title :: " + from);
    Log.e(TAG, "Notification Text :: " + message);


 

}

@RequiresApi(api = Build.VERSION_CODES.P)
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {

    if (sbn.getPackageName().equals(WA_PACKAGE)){

        Log.i("notifyier", String.valueOf(sbn.getNotification().getSmallIcon().getResId()));
        Log.i("notifyier", String.valueOf(sbn.getNotification().extras.get("android.text")));
        int id=sbn.getNotification().getSmallIcon().getResId();
      

    }

}

这工作正常,但在启动完成后一段时间后不起作用,我使用 broadcastreceiver 启动 ListnerService 但仍然无法正常工作

This is working fine but after some time and after boot completes is does not work and i using broadcast receiver to start ListnerService but still it is not working

推荐答案

我已经通过添加几行代码找到了解决方案

I have found solution for this by just adding some lines of code

 @Override
public void onListenerConnected() {
    super.onListenerConnected();
    Log.i("tag","Listner conneted");
    tryReconnectService();
}


 public void tryReconnectService() {
    toggleNotificationListenerService();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        ComponentName componentName =
                new ComponentName(getApplicationContext(), Whatsapp_recorder.class);

        //It say to Notification Manager RE-BIND your service to listen notifications again inmediatelly!
        requestRebind(componentName);
    }
}

/**
 * Try deactivate/activate your component service
 */
private void toggleNotificationListenerService() {
    PackageManager pm = getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName(this, Whatsapp_recorder.class),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    pm.setComponentEnabledSetting(new ComponentName(this, Whatsapp_recorder.class),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}

这篇关于设备 BOOT_COMPLETE 后 NotificationListenerService 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆