Hide notification of foreground service while activity is visible

前端 未结 2 1335
别跟我提以往
别跟我提以往 2021-02-02 01:12

Is their a way to start a service as a foreground service and hide the notification while an activity is visible?

Consider a music player, while the app is opened, you d

2条回答
  •  日久生厌
    2021-02-02 01:49

    Use following steps:

    1.Use ActivityManager to get current package name(i.e the Activity Running on top).

    2.check if its your application then do not show the notifications

    3.else If it is not your application then show the notifications.

    ActivityManager manager =(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                                List tasks = manager.getRunningTasks(1);
                                String topActivityName = tasks.get(0).topActivity.getPackageName();
                                if(!(topActivityName.equalsIgnoreCase("your package name"))){
     //enter notification code here
    }
    

提交回复
热议问题