Androidの通知通知バーの単純な実装は、イベントにサスペンションの通知をクリックしてください

三つの要素の通知

1.小さいアイコン.setSmallIcon()
2.タイトル.setContentTitle()
3.コンテンツ.setContentText()
これは、あなたが通知バーに表示しなければならないことを三つの要素の三つの要素通知バーです

いくつかの一般的に使用される方法
方法 効果
.setSmallIcon() 小さいアイコン
.setContentTitle() 見出し
.setContentText() コンテンツ
.setDefaults(Notification.DEFAULT_ALL)。 モード(両方の音の振動警告灯のALL代わっを)思い出させます
.setLargeIcon() 大きいアイコン
.setContentIntent() ジャンプをクリックします
.setWhen() 通知時間(予告)
.setAutoCancel(ブール値) [閉じる]をクリックします通知するかどうか
.setPriority(Notification.PRIORITY_MAX) 優先順位の通知(MAXは、最も先進的なを表します)
API15は、以下のノートを使用します

1の.java最後のピースタグ.build()メソッドは、置換.getNotificationを(必要)
次のようにnotificationManager.notify(0、builder.getNotificationを());
2. .setPriority()メソッドは使用できません

次は、完全な実装であります

javaファイル

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle("危险警报!!!")
                        .setContentText("您的手机已经被病毒入侵,点击紧急杀毒")
                        .setSmallIcon(R.mipmap.jiaojing)
  notificationManager.notify(0, builder.build()); //API15以及以下需要把.build()方法需要换成.getNotification()          
クリックイベントの通知

Javaコードは、以下
単に.setContentIntent()メソッドの実装を使用して、PendingIntentをインスタンス

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"));
 PendingIntent  pi = PendingIntent.getActivity(context, 0, intent, 0);
 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 Notification.Builder builder = new Notification.Builder(TongZhiActivity.this);
                builder.setContentTitle("标题")
                        .setContentText("内容(点击跳转至百度)")
                        .setSmallIcon(R.mipmap.jiaojing)
                        .setContentIntent(pi)    
                        .setAutoCancel(true)    //点击后关闭通知
                        .setWhen(System.currentTimeMillis()) ;
                notificationManager.notify(1, builder.build());
通知のサスペンションの通知
 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"));
 PendingIntent  pi = PendingIntent.getActivity(context, 0, intent, 0);
 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 Notification.Builder builder = new Notification.Builder(TongZhiActivity.this);
                builder.setContentTitle("悬浮")
                        .setContentText("悬浮通知")
                        .setSmallIcon(R.mipmap.jiaojing)
                        .setContentIntent(pi)
                        .setAutoCancel(true)    //点击后关闭通知
                        .setWhen(System.currentTimeMillis())
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setPriority(Notification.PRIORITY_MAX)
                        .setFullScreenIntent(pi, true);
                notificationManager.notify(3, builder.build());
发布了6 篇原创文章 · 获赞 5 · 访问量 241

おすすめ

転載: blog.csdn.net/qq_44720366/article/details/103495205