在Android中显示默认通知栏

时间:2015-04-22 05:21:47

标签: java android eclipse adt

我在互联网上搜索了很多,但却找不到答案。

我正在开发一个启动器项目,并且点击按钮,我想启动默认的通知栏。是否有 API

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码实现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());

我希望这会有用。