星期一, 十一月 05, 2012

如何在app中3.0 API LEVEL 11后加入notification.

public void setupViews(Context ctx)
{
    Intent notificationIntent = new Intent(ctx, LifeDemo.class);
    PendingIntent contentIntent = PendingIntent.getActivity(ctx,
            0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager nm = (NotificationManager) ctx
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Resources res = ctx.getResources();
    Notification.Builder builder = new Notification.Builder(ctx);

    builder.setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
                .setTicker("Demo Show")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle("Demo Show 1")
                .setContentText("Demo Show2");
    Notification n = builder.build();

    nm.notify(1, n);
}