当我的应用程序触发通知并且设备屏幕关闭时,我一直在尝试展示一个活动,该活动是处理传入电话的屏幕。
目前,在安卓Nougat(华为P9 Lite)设备上可以实现,但在我的安卓Pie设备上(三星Galaxy A20)无法实现。
由于我的应用程序复杂度较高,因此我正在尝试使用更简化的项目来复制此功能。
所以我正在使用这个项目,据我理解,它应该能够满足我的要求。
然而,使用这段代码时,如果应用程序启动并且屏幕关闭,则不会显示活动。 通知将添加到通知区域中。
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)
我已经阅读了这份文档,并且知道在Android 10中有一些限制和解决方案,但现在我只专注于Android Pie及以下版本。当然,解决方案也需要在Android 10上运行。
我有什么遗漏吗?我希望使此示例适用于所有Android版本,以便我可以将解决方案应用于我的应用程序。
任何帮助都将不胜感激。