Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 设备引导完成后NotificationListenerService不工作_Android_Android Studio - Fatal编程技术网

Android 设备引导完成后NotificationListenerService不工作

Android 设备引导完成后NotificationListenerService不工作,android,android-studio,Android,Android Studio,我正在使用NotificationListnerService它在一段时间后工作正常它无法工作,特别是在设备启动后它没有启动我已授予listner所需的所有权限这是我的代码 public class Whatsapp_listner extends NotificationListenerService { private static final String TAG = "NotificationListener"; private static final Stri

我正在使用
NotificationListnerService
它在一段时间后工作正常它无法工作,特别是在设备启动后它没有启动我已授予listner所需的所有权限这是我的代码

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();
      

    }

}

这工作正常,但在一段时间后和引导完成后,它不工作,我使用
广播接收器启动
ListnerService
,但它仍然不工作

我通过添加一些代码行找到了解决方案

 @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);
}

请帮我做这个