gpt4 book ai didi

android - NotificationCompat 和 setFullScreenIntent()

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:43 30 4
gpt4 key购买 nike

我想知道是否有人对这种类型的通知有一些经验。

在我的用例中,我想从我的服务触发一个通知,而不是它应该打开一个全屏视频。 setFullScreenIntent 方法看起来恰好解决了这个问题,因为它在文档中写道:

An intent to launch instead of posting the notification to the status bar. Only for use with extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock that the user has explicitly set to a particular time.

所以它说它像来电一样工作。这意味着即使我的手机处于 sleep 状态,我也应该可以全屏查看通知。

但在我的例子中,我只是收到提醒通知,如果我点击它,它会打开 Activity 。甚至认为在文档中他们提到了一些关于这种行为的事情......

On some platforms, the system UI may choose to display a heads-up notification, instead of launching this intent, while the user is using the device.

... 我想知道如何在触发通知时自动打开 Activity 。就像来电屏幕一样。

这是我的服务代码:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


Intent notificationIntent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.example.test",
"com.example.test.VideoActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent)
.setContentTitle("Video")
.setContentText("Play video")
.setFullScreenIntent(contentIntent, true);

NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);



mNotificationManager.notify(0, mBuilder.build());

return Service.START_STICKY;
}

最佳答案

尝试在onCreate时用setFullScreenIntent通知notification

@Override
public int onCreate() {
Intent notificationIntent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.example.test",
"com.example.test.VideoActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent)
.setContentTitle("Video")
.setContentText("Play video")
.setFullScreenIntent(contentIntent, true);

NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(0, mBuilder.build());
}

关于android - NotificationCompat 和 setFullScreenIntent(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27547395/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com