【问题标题】:NotificationListenerService not working after device BOOT_COMPLETENotificationListenerService 在设备 BOOT_COMPLETE 后不工作
【发布时间】:2020-10-27 16:46:32
【问题描述】:

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

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

    }

}

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

【问题讨论】:

  • 请帮帮我

标签: android android-studio


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    相关资源
    最近更新 更多