gpt4 book ai didi

java - Android 如何在另一个应用程序上显示警报

转载 作者:行者123 更新时间:2023-12-01 13:08:24 25 4
gpt4 key购买 nike

您好,我想在单击我的通知后显示警报。这是代码:

@SuppressWarnings("deprecation")
private void Notify(String notificationTitle, String notificationMessage) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon_stat, "Powiadomionko", System.currentTimeMillis());

notification.ledARGB = Color.CYAN;//0xFFff0000;
notification.ledOnMS = 800;
notification.ledOffMS = 2400;

notification.vibrate = new long[]{100, 120, 100, 120};

Intent notificationIntent = new Intent(this, TerminarzActivity.class);

notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_VIBRATE;

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

notification.setLatestEventInfo(Serwis_updateTERM.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(1, notification);
}

我想在点击后对不在我的 Activity 中的任何内容显示警报。有人知道该怎么做吗?

我知道我必须添加

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

但没有别的了..

最佳答案

检查此通知的创建(取自Geofence示例)。此代码创建一个通知,如果您触摸它,它就会启动您的 MainActivity。

    // Create an explicit content Intent that starts the main Activity
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

// Construct a task stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the main Activity to the task stack as the parent
stackBuilder.addParentStack(MainActivity.class);

// Push the content Intent onto the stack
stackBuilder.addNextIntent(notificationIntent);

// Get a PendingIntent containing the entire back stack
PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

// Get a notification builder that's compatible with platform versions
// >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

// Set the notification contents
builder.setSmallIcon(R.drawable.ic_folder)
.setContentTitle(getString(R.string.geofence_transition_notification_title, transitionType, ids))
.setContentText(getString(R.string.geofence_transition_notification_text))
.setContentIntent(notificationPendingIntent);

// Get an instance of the Notification manager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Issue the notification
mNotificationManager.notify(0, builder.build());

这段代码可能对您有帮助,但这仍然取决于您所说的“警报”是什么意思。

关于java - Android 如何在另一个应用程序上显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23093745/

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