安卓(Android) 怎么通过 Pending Intent 发送通知(Notification)

2019年10月7日22:02:35安卓(Android) 怎么通过 Pending Intent 发送通知(Notification)已关闭评论 715 1170字阅读3分54秒

1.简介

安卓(Android) 怎么通过 Pending Intent 发送通知(Notification)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

 
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;

public class Main{
    private static int sCount = 0;
    public static void fireNotificationWithPendingIntent(
            PendingIntent pendingIntent, Context context) {
        NotificationManager notifyMgr = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notifyMgr.notify(
                1001,
                getCommonBuilder(context)
                        .setContentTitle(
                                context.getString(R.string.app_name))
                        .setContentText(
                                "fireNotificationWithPendingIntent "
                                        + sCount)
                        .setContentIntent(pendingIntent).build());
        sCount++;
    }
    private static NotificationCompat.Builder getCommonBuilder(
            Context context) {
        Uri alarmSound = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        long[] pattern = { 500, 500, 500, 500 };
        return new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher).setSound(alarmSound)
                .setVibrate(pattern).setLights(Color.BLUE, 500, 500)
                .setAutoCancel(true);
    }
}

 
继续阅读
编程十万个怎么办