【发布时间】:2020-01-02 18:05:51
【问题描述】:
当触发通知并且设备屏幕关闭时,我一直在尝试在我的应用中显示活动。此活动是处理来电的屏幕。
到目前为止,这适用于 Android Nougat (Huawei P9 lite) 设备,但不适用于我的 Android Pie 设备 (Samsung Galaxy A20)。
由于我的应用程序的复杂性,我试图用一个更简单的项目来重现它。
所以我正在使用这个project,据我所知,应该做我想做的事。
但是,使用此代码,当应用启动和屏幕关闭时,活动不会显示。通知被添加到通知区域。
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>
我在以下物理设备中对此进行了测试:
- Android Nougat(华为 P9 lite)
- Android Pie 设备(三星 Galaxy A20)
- Android Marshmallow(三星 Galaxy Tab)
我已阅读 this documentation 并且我知道在 Android 10 中有 some limitations 和 a solution 但现在我只关注 Android Pie 及以下版本。当然,该解决方案还需要在 Android 10 上运行。
我错过了什么吗?我想让这个示例适用于所有 Android 版本,以便我可以将解决方案应用于我自己的应用程序。
任何帮助将不胜感激。
【问题讨论】:
标签: android android-notifications android-pendingintent start-activity android-fullscreen