Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to hide all notifications of other apps when my Android app is in foreground. Is this possible? How?

I am creating an educational app where school students can appear for 5 mins quick exams. Most of the time they are using their parent's mobile phone.

Now what I want is that when a student is giving the exam, no notification should come from any other app such as WhatsApp, FB or Gmail.

Is this possible? How?

like image 657
milan m Avatar asked Jun 28 '18 05:06

milan m


3 Answers

One possible solution would be to set the phone in DND (Do Not Disturb) mode. I haven't tested this but according to the documentation you should be able to do:

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int currentFilter = notificationManager.getCurrentInterruptionFilter();
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
// Run the exam...
// Restore the filter
notificationManager.setInterruptionFilter(currentFilter);

This requires a special DND access permission though, and it requires Android M:

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

That permission needs to be accepted by the user via Settings. See this question for more background and how you can launch the activity that leads to this setting.

A second option is to use Screen Pinning, which was introduced in Android L.

like image 80
fejd Avatar answered Sep 26 '22 04:09

fejd


This seems like an interesting question.

Personally, I like the DND idea. It is simple, less messy and effective.

But say we need to reach out to lower APIs, You can use one approach to be like this.

You can have your own Notification Listener.

import android.annotation.SuppressLint;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

@SuppressLint("OverrideAbstract")
public class NotificationListener  extends NotificationListenerService {

    public static String TAG = NotificationListener.class.getSimpleName();

    @Override
    public void onNotificationPosted(StatusBarNotification sbm) {

        /**
         * This condition will define how you will identify your test is running
         * You can have an in memory variable regarding it, or persistant variable,
         * Or you can use Settings to store current state.
         * You can have your own approach
         */
        boolean condition = true; // say your test is running
        
        if(condition)
            cancelAllNotifications(); //Cancel all the notifications . No Disturbance 
        
        //else
            // nothing.
    }
}

Add this to Manifest

 <service android:name=".NotificationListener"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

Now, you will need Notification Access Permission for this. In your application, entry points(or before test as per your logic) you can check this

if (!NotificationManagerCompat.getEnabledListenerPackages(getApplicationContext())
            .contains(getApplicationContext().getPackageName())) {
        //We dont have access
        Intent intent= new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
         //For API level 22+ you can directly use Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivityForResult(intent,NOTIFICATION_REQUEST_CODE);
    } else {
        //Your own logic
        Log.d(TAG, "You have Notification Access");

    }

This intent will open a page like below, which user will have to enable for giving you Access.

enter image description here

Hope this helps.

like image 40
Bhavita Lalwani Avatar answered Sep 29 '22 04:09

Bhavita Lalwani


You can close all notification from other app by using NotificationListenerService:

public class NotificationListener extends NotificationListenerService {

    @Override
    public void onNotificationPosted(StatusBarNotification statusBarNotification) {
      cancelNotification(statusBarNotification.getKey());
    }
}

You must declare the service in your manifest file with the Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE permission and include an intent filter with the SERVICE_INTERFACE action. For example:

 <service android:name=".NotificationListener"
          android:label="@string/service_name"
          android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
     <intent-filter>
         <action android:name="android.service.notification.NotificationListenerService" />
     </intent-filter>
 </service>

https://developer.android.com/reference/android/service/notification/NotificationListenerService.html

like image 1
Anisuzzaman Babla Avatar answered Sep 29 '22 04:09

Anisuzzaman Babla