如何停止Android的NotificationListenerService?

dly7yett  于 2023-05-27  发布在  Android
关注(0)|答案(5)|浏览(330)

我在我的android应用程序中使用的是从kpbird's example开发的NotificationListenerService。即使在应用程序被销毁后,该服务也会在后台运行。有没有办法在应用程序启动时停止此服务并启动它?

wpcxdonn

wpcxdonn1#

NotificationListenerService无法停止,因为在我们启动服务系统后,系统将调用bindService()。服务将保持ServiceConnection,然后它将不响应stopService或stopSelf。
作为我的搜索结果,我们也不能从服务示例中删除ServiceConnection。

fdbelqdn

fdbelqdn2#

在服务内部创建一个广播接收器,它扩展了NotificationListenerService,如下所示

public class Block_All_Notification2 extends NotificationListenerService {

boolean check=false;

CancelNotificationReceiver mReceiver = new CancelNotificationReceiver();
public static final String Package_Name = "com.muhaiminurabir.notificationblocker";
@Override
public IBinder onBind(Intent intent) {
    return super.onBind(intent);
}

@Override
public void onNotificationPosted(StatusBarNotification sbn){
    // Implement what you want here
    // Inform the notification manager about dismissal of all notifications.
    Log.d("Msg", "Notification arrived");
    start_blocking();
   /* if (false){
        cancelAllNotifications();
    }*/
    //Block_All_Notification2.this.cancelAllNotifications();
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn){
    // Implement what you want here
    Log.d("Msg", "Notification Removed");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    String block = intent.getStringExtra("block");
    Log.d("checking service 1",block+"");
    if (block.equals("yes")){
        check=true;
    }else if(block.equals("no")){
        check=false;
    }
    Log.d("checking service 1",check+"");
    return START_STICKY;

}

@Override
public void onCreate() {
    super.onCreate();
    IntentFilter filter = new IntentFilter();
    filter.addAction(Package_Name);
    registerReceiver(mReceiver, filter);
}

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(mReceiver);
}

public void start_blocking(){
    Log.d("checking service",check+"");
    if (check==true){
        cancelAllNotifications();
    }
}

class CancelNotificationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("received service","received");
        String action;
        if (intent != null && intent.getAction() != null) {
            action = intent.getAction();
            if (action.equals(Package_Name)) {
                String block = intent.getStringExtra("block");
                if (TextUtils.equals(block, "no")) {
                    Log.d("checking service 1",block+"");
                    if (block.equals("yes")){
                        check=true;
                        cancelAllNotifications();
                    }else if(block.equals("no")){
                        check=false;
                    }
                    Log.d("checking service 1",check+"");
                } else if (TextUtils.equals(block, "yes")) {
                    Log.d("checking service 1",block+"");
                    if (block.equals("yes")){
                        check=true;
                        cancelAllNotifications();
                    }else if(block.equals("no")){
                        check=false;
                    }
                    Log.d("checking service 1",check+"");
                }
            }
        }
    }

}

}

并在您的活动中添加此代码以与上面创建的服务进行通信

Intent intent = new Intent();
        intent.setAction(Block_All_Notification2.Package_Name);
        intent.putExtra("block", "no");
        context.sendBroadcast(intent);
tyu7yeag

tyu7yeag3#

也许这个问题已经解决了。
但是,我最近发现了一种启动或停止NotificationListenerService的方法,我想分享一下。
当然,这不是我认为的官方方法。
所以,我觉得你可以参考一下,也是有这个办法的。
为了运行NotificationListenerService,必须手动授权用户,这一事实给了我一个提示。
当按下Start Button时,显示NotificationListener权限窗口,并显示Toast消息以引导用户请求权限。
相反,当按下Stop Button时,显示NotificationListener权限窗口,同时显示Toast消息,引导用户撤销权限。
我的代码如下。

main_start_service_btn.setOnClickListener {
    if(isNotificationPermissionAllowed()) {
        Toast.makeText(this, "Already Starting Service.", Toast.LENGTH_LONG).show()
    }
    else {
        Toast.makeText(this, "Please allow permission in the next window", Toast.LENGTH_LONG).show()
                                                            
startActivity(Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"))
    }
}

main_stop_service_btn.setOnClickListener {
    if(isNotificationPermissionAllowed()) {
        Toast.makeText(this, "Please deny permission in the next window", Toast.LENGTH_LONG).show()
                startActivity(Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"))
    }
    else {
        Toast.makeText(this, "Already Stopped Service.", Toast.LENGTH_LONG).show()
    }
}
wooyq4lh

wooyq4lh4#

要终止服务,您需要请求android运行时解除与服务的绑定。使用requestUnbind()

jjhzyzn0

jjhzyzn05#

NotificationListenerService扩展了Service,所以你可以通过调用Service类中的stopSelf()方法来停止服务。
查看NotificationListenerServiceServices

相关问题