Day03Notitfcation通知

圆形头像

 Glide.with(this).load(R.mipmap.ic_launcher).apply(new RequestOptions().circleCrop()).into(yuan);

1.双击退出

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK ){
            //判断用户两次按键的时间差是否在一个预期值之内,是的话直接直接退出,不是的话提示用户再按一次后退键退出。
            if(System.currentTimeMillis() - exitTime > 2000){
                Toast.makeText(this,"在点就退出",Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
                //当返回true时,表示已经完整地处理了这个事件,并不希望其他的回调方法再次进行处理,而当返回false时,
                // 表示并没有完全处理完该事件,更希望其他回调方法继续对其进行处理,
                return true;
            }else{
                finish(); //结束当前activity
            }
        }
        return super.onKeyDown(keyCode, event);
    }

2.自定义通知

package com.example.day003;

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

import java.math.RoundingMode;


/**
 * 任务栏通知
 * 风一样的男人
 */
public class NotificationActivity extends AppCompatActivity {
    private Button sendMessageId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        sendMessageId = findViewById(R.id.send_message_id);
        sendMessageId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //定义一个最简单通知的方法(无交互)
//                sendNotification();
                //定义一个有交互的通知的方法
//                sendActionNotification();
                //自定义一个通知
                userNotification();
            }
        });

    }

    /**
     * 自定义一个通知
     */
    private void userNotification() {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentText("内容");
        builder.setContentTitle("头部");
        /**
         * RemoteViews是可以在别的进程(系统进程)中显示的View,并且提供了一组跨进程更新它界面的操作
         * 两个参数,第一个布局所在包名
         * 第二个是布局Id
         * 布局文件是自己创建的,随便一个线性布局,加一个textView和ImageView即可
         */
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.simple_layout);
        /**
         * 由于运行在不同的进程中,所以RemoteViews无法像正常的View一样更新UI。
         * RemoteViews提供了一系列的set方法,但是这些set方法只是View全部方法的子集。
         */

        //都是两个参数,第一个参数相当于findViewById,第二个是设置一个值.
        remoteViews.setTextViewText(R.id.rm_text_id,"十七岁的老冯");
        remoteViews.setImageViewResource(R.id.rm_image_id,R.mipmap.ic_launcher);

        //builder.setContent(remoteViews);//过期
         builder.setCustomContentView(remoteViews)
        Notification build = builder.build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(2,build);

    }

3.最简单的通知

package com.example.day003;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


/**
 * 任务栏通知
 * 风一样的男人
 */
public class NotificationActivity extends AppCompatActivity {
    private Button sendMessageId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        sendMessageId = findViewById(R.id.send_message_id);
        sendMessageId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //定义一个发送通知的方法
                sendNotification();
            }
        });
    }

    /**
     * 最简单的一个通知
     */
    private void sendNotification() {
        //创建构造者
        Notification.Builder builder = new Notification.Builder(this);
        //设置属性   setSamllIcon该属性必须设置
        builder.setSmallIcon(R.mipmap.ic_launcher); //必须设置
        builder.setContentTitle("我是标题"); //建议设置
        builder.setContentText("我是内容"); //建议设置

//        builder.setTicker("我是提示信息");
//        builder.setContentInfo("我是附加信息"); //7.0以后已经过期

        //创建对象.发送的就是这个对象
        Notification build = builder.build();
        //获取通知管理器,负责发通知、清除通知等
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //TODO :发送通知
        //参数一 id 通知的id(稍后介绍意义)   参数二 通知对象
        notificationManager.notify(1,build);
    }
}


4.进度条通知

//进度条通知
    private void progress_notification() {
        final NotificationManager manager= (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        //TODO 2:创建构造者
        final Notification.Builder builder = new Notification.Builder(this);
        //TODO 3:设置属性   setSamllIcon该属性必须设置
        builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
        builder.setContentTitle("我是标题");

        //TODO 设置进度条
        //参数一 最大值 参数二:当前进度 参数三 是否模糊
        //    builder.setProgress(100,50,true);
        final Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            int progress;
            @Override
            public void run() {
                //1.模拟下载过程
                builder.setContentText("正在下载,当前进度"+progress);
                builder.setProgress(100,progress,false);//确定的进度条
                progress+=10;
                manager.notify(6,builder.build());
                if(progress==100){
                    //2.安装过程
                    builder.setContentText("正在安装");
                    builder.setProgress(0,0,true);//安装模糊
                    manager.notify(6,builder.build());
                    try {
                        Thread.sleep(7000);//模拟安装过程
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //3.安装完成
                    manager.cancel(6);//取消置顶的通知
                    timer.cancel();
                }
            }
        }, 0, 1000);
    }


5.锁屏通知

//builde的时候 
//通过 setVisibility() 方法设置即可
.setVisibility(VISIBILITY_PUBLIC)
.build();

6.大屏通知和列表通知

 /**
     * 通知的样式
     */
    private void notificationStyle() {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("列表通知");
//        builder.setContentTitle("大图通知");

        //通知内容为大图片
        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
        bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));

        //通知内容为列表显示
        Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
        inboxStyle.addLine("李白");
        inboxStyle.addLine("猴子");
        inboxStyle.addLine("露娜");

//        builder.setStyle(bigPictureStyle);
        builder.setStyle(inboxStyle);
        //不能跨APP
        Intent intent = new Intent(this, MainActivity.class);
        //intent - PendingIntent
        PendingIntent intent1 = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
        builder.setFullScreenIntent(intent1, true);

        builder.setContentIntent(intent1);
        manager.notify(9, builder.build());
    }

7.设置通知效果

 // 在builder的时候加上如下属性即可.
 builder.setDefaults(Notification.DEFAULT_ALL);

//设置系统默认提醒效果,一旦设置默认提醒效果,则自定义的提醒效果会全部失效。具体可看源码
//添加默认震动效果,需要申请震动权限
//<uses-permission android:name="android.permission.VIBRATE" />
Notification.DEFAULT_VIBRATE
 
//添加系统默认声音效果,设置此值后,调用setSound()设置自定义声音无效
Notification.DEFAULT_SOUND
 
//添加默认呼吸灯效果,使用时须与 Notification.FLAG_SHOW_LIGHTS 结合使用,否则无效
Notification.DEFAULT_LIGHTS
 
//添加上述三种默认提醒效果
Notification.DEFAULT_ALL

各个属性进阶版

/**
* 最普通的通知效果
*/
private void showNotifyOnlyText() {
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setLargeIcon(mLargeIcon)
           .setContentTitle("我是只有文字效果的通知")
           .setContentText("我没有铃声、震动、呼吸灯,但我就是一个通知");
   mManager.notify(1, builder.build());
}
 
/**
* 展示有自定义铃声效果的通知
* 补充:使用系统自带的铃声效果:Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
*/
private void showNotifyWithRing() {
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是伴有铃声效果的通知")
           .setContentText("美妙么?安静听~")
           //调用系统默认响铃,设置此属性后setSound()会无效
           //.setDefaults(Notification.DEFAULT_SOUND)
           //调用系统多媒体裤内的铃声
           //.setSound(Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2"));
           //调用自己提供的铃声,位于 /res/values/raw 目录下
           .setSound(Uri.parse("android.resource://com.littlejie.notification/" + R.raw.sound));
   //另一种设置铃声的方法
   //Notification notify = builder.build();
   //调用系统默认铃声
   //notify.defaults = Notification.DEFAULT_SOUND;
   //调用自己提供的铃声
   //notify.sound = Uri.parse("android.resource://com.littlejie.notification/"+R.raw.sound);
   //调用系统自带的铃声
   //notify.sound = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2");
   //mManager.notify(2,notify);
   mManager.notify(2, builder.build());
}
 
/**
* 展示有震动效果的通知,需要在AndroidManifest.xml中申请震动权限
* <uses-permission android:name="android.permission.VIBRATE" />
* 补充:测试震动的时候,手机的模式一定要调成铃声+震动模式,否则你是感受不到震动的
*/
private void showNotifyWithVibrate() {
   //震动也有两种设置方法,与设置铃声一样,在此不再赘述
   long[] vibrate = new long[]{0, 500, 1000, 1500};
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是伴有震动效果的通知")
           .setContentText("颤抖吧,凡人~")
           //使用系统默认的震动参数,会与自定义的冲突
           //.setDefaults(Notification.DEFAULT_VIBRATE)
           //自定义震动效果
           .setVibrate(vibrate);
   //另一种设置震动的方法
   //Notification notify = builder.build();
   //调用系统默认震动
   //notify.defaults = Notification.DEFAULT_VIBRATE;
   //调用自己设置的震动
   //notify.vibrate = vibrate;
   //mManager.notify(3,notify);
   mManager.notify(3, builder.build());
}
 
/**
* 显示带有呼吸灯效果的通知(####)
*/
private void showNotifyWithLights() {
   final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是带有呼吸灯效果的通知")
           .setContentText("一闪一闪亮晶晶~")
           //ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间
           .setLights(0xFF0000, 3000, 3000);
   Notification notify = builder.build();
   //只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持呼吸灯提醒。
   notify.flags = Notification.FLAG_SHOW_LIGHTS;
   //设置lights参数的另一种方式
   //notify.ledARGB = 0xFF0000;
   //notify.ledOnMS = 500;
   //notify.ledOffMS = 5000;
   //使用handler延迟发送通知,因为连接usb时,呼吸灯一直会亮着
   Handler handler = new Handler();
   handler.postDelayed(new Runnable() {
       @Override
       public void run() {
           mManager.notify(4, builder.build());
       }
   }, 10000);
}
 
/**
* 显示带有默认铃声、震动、呼吸灯效果的通知
* 如需实现自定义效果,请参考前面三个例子
*/
private void showNotifyWithMixed() {
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是有铃声+震动+呼吸灯效果的通知")
           .setContentText("我是最棒的~")
           //等价于setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
           .setDefaults(Notification.DEFAULT_ALL);
   mManager.notify(5, builder.build());
}
 
/**
* 通知无限循环,直到用户取消或者打开通知栏(其实触摸就可以了),效果与FLAG_ONLY_ALERT_ONCE相反
* 注:这里没有给Notification设置PendingIntent,也就是说该通知无法响应,所以只能手动取消
*/
private void showInsistentNotify() {
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是一个死循环,除非你取消或者响应")
           .setContentText("啦啦啦~")
           .setDefaults(Notification.DEFAULT_ALL);
   Notification notify = builder.build();
   notify.flags |= Notification.FLAG_INSISTENT;
   mManager.notify(6, notify);
}
 
/**
* 通知只执行一次,与默认的效果一样
*/
private void showAlertOnceNotify() {
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("仔细看,我就执行一遍")
           .setContentText("好了,已经一遍了~")
           .setDefaults(Notification.DEFAULT_ALL);
   Notification notify = builder.build();
   notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
   mManager.notify(7, notify);
}
 
/**
* 清除所有通知
*/
private void clearNotify() {
   mManager.cancelAll();
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值