简体   繁体   中英

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:

  • Android Nougat (Huawei P9 lite)
  • Android Pie device (Samsung Galaxy A20)
  • Android Marshmallow (Samsung Galaxy Tab)

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.

首先,您必须向AndroidManifest.xml添加权限:

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

USE_FULL_SCREEN_INTENTSYSTEM_ALERT_WINDOW权限添加到您的 AndroidManifest.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM