首页 > 系统 > Android > 正文

android 浮现Notification demo

2019-11-06 09:35:21
字体:
来源:转载
供稿:网友

官方中文文档: https://developer.android.google.cn/guide/topics/ui/notifiers/notifications.html

import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.os.Build;import android.os.Bundle;import android.support.annotation.RequiresApi;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.view.View;/** * Notification 对象必须包含以下内容: * <p> * 小图标,由 setSmallIcon() 设置 * 标题,由 setContentTitle() 设置 * 详细文本,由 setContentText() 设置 */public class MainActivity extends AppCompatActivity { @RequiresApi(api = Build.VERSION_CODES.LOLLipOP) @Override PRotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void show(View view) { NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("标题") .setContentText("内容") .setGroupSummary(true); Intent notifyIntent = new Intent(this, Notify.class); PendingIntent notifyPendingIntent = PendingIntent.getActivity( this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT ); //浮窗设置 builder.setFullScreenIntent(notifyPendingIntent, true); // Puts the PendingIntent into the notification builder builder.setContentIntent(notifyPendingIntent); final Notification notification = builder.build(); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(1, notification); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表