gpt4 book ai didi

java - 方向改变和通知

转载 作者:行者123 更新时间:2023-11-30 09:42:25 27 4
gpt4 key购买 nike

我想实现以下场景:

当用户按下 Home 键时,状态栏中会显示一条通知,而应用程序通常是隐藏的。当用户点击时,通知应用程序通常会恢复。

这是我的代码:

private int NOTIFICATION = R.string.local_service_started;
private NotificationManager mNM;

private void showNotification() {
CharSequence text = getText(R.string.local_service_started);
Notification notification = new Notification(R.drawable.icon, text, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, JingleCartoucheActivity.class), 0);
notification.setLatestEventInfo(this, getText(R.string.local_service_label), text, contentIntent);
mNM.notify(NOTIFICATION, notification);
}

@Override
public void onPause() {
super.onPause();
if (!isFinishing()) {
showNotification();
}
}

@Override
public void onResume() {
super.onResume();
mNM.cancel(NOTIFICATION);
}

除一件事外,所有这些都运行良好:

当应用程序运行且用户旋转手机时,通知会创建并立即销毁。有没有办法检测 onPause() 中的手机方向变化而不显示此通知?

最佳答案

您可以使用 getResources().getConfiguration().orientation 获取当前方向。在 onCreate 中,您存储方向值,然后您可以稍后比较它。请务必将值存储到静态变量中,因为当您旋转手机时, Activity 会被销毁,它的值也会被销毁。

关于java - 方向改变和通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8701193/

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