安卓(Android) 怎么从标题和文本创建通知(Notification)

2019年10月8日23:11:01安卓(Android) 怎么从标题和文本创建通知(Notification)已关闭评论 327 943字阅读3分8秒

1.简介

安卓(Android) 怎么从标题和文本创建通知(Notification)?——请看下文。

2.示例代码

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

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.preview.support.v4.app.NotificationManagerCompat;
import android.preview.support.wearable.notifications.WearableNotifications;
import android.support.v4.app.NotificationCompat;

public class Main{
    public static void createNotification(Activity a, String title,
            String text) {
        int notificationId = 001;
        
        Intent viewIntent = new Intent(a, WearActivity.class);
        PendingIntent viewPendingIntent = PendingIntent.getActivity(a, 0,
                viewIntent, 0);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                a).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(title).setContentText(text)
                .setContentIntent(viewPendingIntent);

        
        NotificationManagerCompat notificationManager = NotificationManagerCompat
                .from(a);

        
        notificationManager.notify(notificationId,
                notificationBuilder.build());
    }
}

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