簡體   English   中英

在Android中顯示默認通知欄

[英]Show default notification bar in Android

我在互聯網上搜索了很多內容,卻找不到答案。

我正在啟動項目,並且單擊一個按鈕 ,我想啟動默認通知欄 是否有任何API

您可以使用以下代碼實現android默認通知。 您可以自定義。

// Invoking the default notification service
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this);

    mBuilder.setContentTitle("ur titlw");
    mBuilder.setContentText("ur content");
    mBuilder.setTicker("ur sticker");
    mBuilder.setSmallIcon(R.drawable.uricon);

    // Increase notification number every time a new notification arrives
    mBuilder.setNumber(++numMessagesOne);

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ur activity where this notification shout appear);
    resultIntent.putExtra("notificationId", "test");

    // This ensures that navigating backward from the Activity leads out of
    // the app to Home page
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Adds the back stack for the Intent
    stackBuilder.addParentStack(ur activity where it returns when click on the notification);

    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_ONE_SHOT // can only be used once
            );
    // start the activity when the user clicks the notification text
    mBuilder.setContentIntent(resultPendingIntent);

    myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // pass the Notification object to the system
    myNotificationManager.notify(notificationIdOne, mBuilder.build());

我希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM