《进阶之光》笔记(3)——三种Notification

package com.zjw.chapter1androidcharacteristic;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//1.1.4 p18 3种Notification
//布局三个Button
/*
普通、折叠式、悬挂式
 */
/*
1.普通Notification
创建Builder对象,PendingIntent控制跳转
 */
/*
2.折叠式Notification
折叠式Notification是一种自定义视图的Notification,用来显示长文本和一些自定义的布局场景
它有两种状态:
一:普通状态下的视图(如果不是自定义与普通Notification一样)
二:展开状态下的视图,需要自定义视图
这个视图显示的进程和创建视图的不是一个进程,需要使用RemoteViews
 */
/*
3.悬挂式Notification
悬挂式Notification是5.0新增方式
不需要下拉通知栏就直接显示出来悬挂在屏幕上方,焦点仍在用户操作界面,过几秒自动消失
调用setFullScreenIntent
 */
/*
通知的显示等级:
5.0加入了显示等级
VISIBILITY_PUBLIC:任何情况都会显示通知
VISIBILITY_PRIVATE:只有在没有锁屏时会显示通知
VISIBILITY_SECRET:只有在pin,password等安全锁和没有锁屏时会显示通知
调用builder.setVisibility(Notification.VISIBILITY_PUBLIC);
 */
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btn1)
    Button mBtn1;
    @BindView(R.id.btn2)
    Button mBtn2;
    @BindView(R.id.btn3)
    Button mBtn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn1, R.id.btn2, R.id.btn3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                initNotification1();
                break;
            case R.id.btn2:
                initNotification2();
                break;
            case R.id.btn3:
                initNotification3();
                break;
        }
    }

    private void initNotification1() {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"));
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

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

        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("普通通知")
                .setContentText("This is content text")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
                .setContentIntent(pi)
                .setAutoCancel(true)
//                        .setSound(Uri.fromFile(new File("/system/media.audio/ringtones/Luna.ogg")))
//                        .setVibrate(new long[]{0,1000,1000,1000})
//                        .setLights(Color.GREEN,1000,1000)
//                        .setDefaults(NotificationCompat.DEFAULT_ALL)
//                        .setStyle(new NotificationCompat.BigTextStyle().bigText("Learn how to" +
//                                "buile notifications,send and sync data, and use voice actions." +
//                                "Get the official Android IDE and developer tools to build apps for Android"))
//                        .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.c)))
//                .setPriority(NotificationCompat.PRIORITY_MAX)
                .build();

        notificationManager.notify(1, notification);
    }

    private void initNotification2() {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"));
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

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

        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("折叠式通知")
                .setContentText("This is content text")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
                .setContentIntent(pi)
                .setAutoCancel(true)
//                        .setSound(Uri.fromFile(new File("/system/media.audio/ringtones/Luna.ogg")))
//                        .setVibrate(new long[]{0,1000,1000,1000})
//                        .setLights(Color.GREEN,1000,1000)
//                        .setDefaults(NotificationCompat.DEFAULT_ALL)
//                        .setStyle(new NotificationCompat.BigTextStyle().bigText("Learn how to" +
//                                "buile notifications,send and sync data, and use voice actions." +
//                                "Get the official Android IDE and developer tools to build apps for Android"))
//                        .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.c)))
//                .setPriority(NotificationCompat.PRIORITY_MAX)
                .build();

        //使用RemoteViews创建自定义视图
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notificaation_view);
        //指定展开时视图
        notification.bigContentView = remoteViews;
        //也可以指定普通状态时的视图
        //notification.contentView=remoteViews;

        notificationManager.notify(2, notification);
    }

    private void initNotification3() {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"));
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

        Intent intent2 = new Intent();
        intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent2.setClass(this, MainActivity.class);
        PendingIntent pi2 = PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_CANCEL_CURRENT);

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

        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("悬挂式通知")
                .setContentText("This is content text")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
                .setContentIntent(pi)
                .setAutoCancel(true)
//                        .setSound(Uri.fromFile(new File("/system/media.audio/ringtones/Luna.ogg")))
//                        .setVibrate(new long[]{0,1000,1000,1000})
//                        .setLights(Color.GREEN,1000,1000)
//                        .setDefaults(NotificationCompat.DEFAULT_ALL)
//                        .setStyle(new NotificationCompat.BigTextStyle().bigText("Learn how to" +
//                                "buile notifications,send and sync data, and use voice actions." +
//                                "Get the official Android IDE and developer tools to build apps for Android"))
//                        .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.c)))
//                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setFullScreenIntent(pi2, true)
                .build();

        notificationManager.notify(3, notification);
    }
}

转载于:https://my.oschina.net/u/3620480/blog/1511556

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值