amp
amp

Reputation: 12352

Notification with setFullScreenIntent not displaying activity when screen is off

I have been trying to show an activity in my app when a notification is triggered and the device screen is off. This activity is a screen for handling an incoming call.

So far, this is working with an Android Nougat (Huawei P9 lite) device, but not working with my Android Pie device (Samsung Galaxy A20).

Due to the complexity of my app, I am trying to reproduce this with a simpler project.

So I am using the this project that as far as I understand, should do what I want.

However with this code, the activity is not displayed when the app start and if the screen is off. The notification is added to the notification area.

MainActivity.java

public class MainActivity extends Activity {
  private static final String CHANNEL_WHATEVER="channel_whatever";
  private static final int NOTIFY_ID=1337;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    NotificationManager mgr=
        (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O &&
      mgr.getNotificationChannel(CHANNEL_WHATEVER)==null) {
      mgr.createNotificationChannel(new NotificationChannel(CHANNEL_WHATEVER,
        "Whatever", NotificationManager.IMPORTANCE_DEFAULT));
    }

    mgr.notify(NOTIFY_ID, buildNormal().build());

    finish();
  }

  private NotificationCompat.Builder buildNormal() {
    NotificationCompat.Builder b=
      new NotificationCompat.Builder(this, CHANNEL_WHATEVER);

    b.setAutoCancel(true)
     .setDefaults(Notification.DEFAULT_ALL)
     .setContentTitle(getString(R.string.download_complete))
     .setContentText(getString(R.string.fun))
     .setContentIntent(buildPendingIntent(Settings.ACTION_SECURITY_SETTINGS))
     .setSmallIcon(android.R.drawable.stat_sys_download_done)
      .setNumber(5)
     .setFullScreenIntent(buildPendingIntent(Settings.ACTION_DATE_SETTINGS), true)
     .addAction(android.R.drawable.ic_media_play,
         getString(R.string.play),
         buildPendingIntent(Settings.ACTION_SETTINGS));

    return(b);
  }

  private PendingIntent buildPendingIntent(String action) {
    Intent i=new Intent(action);

    return(PendingIntent.getActivity(this, 0, i, 0));
  }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.commonsware.android.fullscreen"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="15"/>
<supports-screens android:largeScreens="true" android:smallScreens="true" android:normalScreens="true" android:xlargeScreens="true"/>
<uses-permission android:name="android.permission.VIBRATE"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name="MainActivity"
        android:theme="@android:style/Theme.Translucent.NoTitleBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

</manifest>

I tested this in the following physical devices:

I have read this documentation and I know that in Android 10 there are some limitations and a solution but for now I am only focusing on Android Pie and below. Of course then the solution will also need to work on Android 10.

Am I missing something? I would like to make this example work for all Android versions so that I can apply the solution to my own app.

Any help would be greatly appreciated.

Upvotes: 4

Views: 3289

Answers (2)

nzjoel
nzjoel

Reputation: 1206

Add both USE_FULL_SCREEN_INTENT and SYSTEM_ALERT_WINDOW permissions to your AndroidManifest.xml

Upvotes: 0

Vic
Vic

Reputation: 191

First you have to add the permission to the AndroidManifest.xml:

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

Upvotes: 5

Related Questions