Android_通知

1. 通知的用法和讲解

        //点击通知可以跳转 PendingIntent
        Intent intent = new Intent(this, Main2Activity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

        //获取到NotificationManager实例  用来管理通知
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //随着Android版本的不同,每次升级对Notification的API都做修改,support.v4 中NotificationCompat 做了兼容
        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
        builder.setContentTitle("通知");
        builder.setContentText("这是一条通知");
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setContentIntent(pendingIntent);//用来点击通知来进行跳转的
        builder.setAutoCancel(true);//设置这个语句 在点击通知后,可以将通知从通知栏上取消。
        // 另一个取消通知的方法 是 在跳转到的界面进行取消,
        //NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        //manager.cancel(1)  1 表示在 notify()方法中设置唯一标识。
        Notification notification = builder.build();
        notificationManager.notify(1, notification);

        //******  进阶作用  ******
        //在 NotificationCompat.Builder 中有很多API

        //1.设置来通知时的铃声
        builder.setSound(Uri.fromFile(new File("system/media/audio/ringtones/luna.ogg")));

        //2. 来通知时候手机振动 数组中参数解释 角标为0代表静止时间 角标为1 代表振动时长 以此类推。
        builder.setVibrate(new long[]{0,1000,1000,1000});
        //想要振动 就得进行权限的声明 <uses-permission android:name = "android.permission.VIBRATE"/>

        //3. LED灯的闪烁 参数1:指示灯的颜色  参数二:亮的时长 参数三:熄灭的时长
        builder.setLights(Color.GREEN,1000,1000);

        // 默认配置 系统根据当前手机环境 来进行铃声 振动的配置
        builder.setDefaults(NotificationCompat.DEFAULT_ALL);

        //******  高级作用   ******
        //1. setStyle() 设置样式
        //1)长文字的完全展示
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText("**************************************************************************************************************************" +
                "****************************************************************************************"));
        //2)展示大图片
        builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)));

        //2. setPriority() 设置优先级
        //五个等级 NotificationCompat.PRIORITY_MAX//HIGH/LOW/DEFAULT/MIN
        builder.setPriority(NotificationCompat.PRIORITY_MAX);

2. 折叠式通知

 /**
     * 展示折叠的通知  使用RemoveViews :因为创建视图的进程和展示视图的进程不在一个进程里面
     */
    private void showFoldNotification() {


        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.remoteview);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://blog.csdn.net/dashingqi"));
        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        builder.setContentTitle("折叠式通知");
        builder.setAutoCancel(true);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(pendingIntent);
        Notification notification = builder.build();
        //指定展开时的视图  bigContentView 只有在api16 以上才好用。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            notification.bigContentView = remoteViews;
        notificationManager.notify(1, notification);


    }

3. 悬挂式通知栏

/**
     * 悬挂式通知栏
     */
    private void showScreenNotification() {

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.csdn.net/dashingqi"));
        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setAutoCancel(true);
        builder.setContentTitle("悬挂式通知");
        //设置点击跳转
        Intent hangIntent = new Intent();
        hangIntent.setClass(this,NotificationActivity.class);
        hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent hangPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        //设置悬浮通知
        builder.setFullScreenIntent(hangPendingIntent,true);

        Notification notification = builder.build();
        notificationManager.notify(2,notification);
        
    }

猜你喜欢

转载自blog.csdn.net/dashingqi/article/details/81124479