小编典典

如何在android中显示简单通知?

java

如何在Android的通知栏中显示简单的通知?请帮助我提供最简单的解决方案。


阅读 359

收藏
2020-12-03

共1个答案

小编典典

我假设您是在Notificationbar中询问通知。如果是这样,请尝试以下代码,

private void showNotification(String eventtext, Context ctx) {



    // Set the icon, scrolling text and timestamp
    Notification notification = new Notification(R.drawable.noti_icon,
            text, System.currentTimeMillis());

    // The PendingIntent to launch our activity if the user selects this
    // notification
    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
            new Intent(ctx, MainActivity.class), 0);

    // Set the info for the views that show in the notification panel.
    notification.setLatestEventInfo(ctx, "Title", eventtext,
            contentIntent);

    // Send the notification.
    mNotificationManager.notify("Title", 0, notification);
}
2020-12-03