应用程序崩溃后,通知侦听器服务无法运行

问题描述 投票:4回答:2

我的应用程序有问题,我想报告此错误。

我开发了可以使用NotificationListenerService抓取通知的应用程序。

它运作良好。

但NotificationListenerService类有我认为的问题。

因为,如果应用程序崩溃,应用程序根本无法抓取通知,直到手机重新启动。

谁能解决这个问题?

请帮我。

这个bug很清楚!!但要找到解决方案并不容易......

android notifications crash reboot notification-listener
2个回答
8
投票

如果您已经拥有权限,那么:

在您的服务类或其他服务/活动中,您可以切换“组件能力”以收听通知:

    public void tryReconnectService() {
        toggleNotificationListenerService();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            ComponentName componentName =
                    new ComponentName(getApplicationContext(), NotificationReaderV2Service.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, NotificationReaderV2Service.class),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        pm.setComponentEnabledSetting(new ComponentName(this, NotificationReaderV2Service.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }

您的通知监听器是一个SERVICE,它可以被系统杀死,您可以像FOREGROUND一样进行服务,从而大大降低系统终止服务的可能性。

@Override
    public void onListenerConnected() {
        super.onListenerConnected();
        Log.d(TAG, "Service Reader Connected");
    Notification not = createNotification();
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (mNotificationManager != null) {
        mNotificationManager.notify(NOTIFICATION_ID, not);
    }

    startForeground(NOTIFICATION_ID, not);

    //Alarm to auto - send Intents to Service to reconnect, you can ommit next line.
    alarmIt();
}

如果你更喜欢“安全”,你可以编程不友好的电池警报,请尝试使用不准确的警报,用户的电池会很高兴:

private void alarmIt() {
    Log.d(TAG, "ALARM PROGRAMMATED at"+HotUtils.formatDate(new Date()));
    Calendar now = Calendar.getInstance();
    now.setTimeInMillis(System.currentTimeMillis());
    now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + 1);

    Intent intent = new Intent(this, NotificationReaderV2Service.class);
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    intent.setAction(REBIND_ACTION);

    PendingIntent pendingIntent = PendingIntent.getService(this, 0,
            intent, 0);

    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    //The alarms that are repeated are inaccurate by default, use RTC_WAKE_UP at your convenience.
    //Alarm will fire every minute, CHANGE THIS iF DO YOU CAN, you can't use less than 1 minute to repeating alarms.
    manager.setRepeating(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), 1000 * 60 * 1, pendingIntent);
}

然后读取Intent以重新连接服务绑定:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "Notification service onStartCommandCalled");
    if (intent!=null && !HotUtils.isNullOrEmpty(intent.getAction()) && intent.getAction().equals(REBIND_ACTION)){
        Log.d(TAG, "TRYING REBIND SERVICE at "+HotUtils.formatDate(new Date()));
        tryReconnectService();//switch on/off component and rebind
    }
    //START_STICKY  to order the system to restart your service as soon as possible when it was killed.
    return START_STICKY;
}

请记住,执行所有这些步骤可以确保您的服务无论如何都会被系统杀死,但是此代码将重新启动服务并使其更难以杀死它。

也许,您应该考虑将PARTIAL_WAKE_LOCK与您的服务一起使用,并在一个进程中独立执行它(:remote),如果您想要更加确定(也许这是无用的)

我想添加一个经常出现的常见错误,不要覆盖onBind和onUnbind方法或覆盖INTENT ACTION。这将导致您的服务无法连接并且永远不会在onListenerConnected上运行保持Intent不变,在大多数情况下您不需要编辑它。


0
投票

我在这看到完全相同。我发现的唯一“解决方案”是让通知监听器在一个单独的进程中运行。然后,如果应用程序的其余部分崩溃,它不会停止监听器。因此,只有特定的通知侦听器服务崩溃才需要重新启动。

虽然看起来是一个糟糕而复杂的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.