zoukankan      html  css  js  c++  java
  • 自定义通知Notification:自己定义通知Notification下拉后的显示样式

    注意:以下有些方法需要在build.gradle里修改minSdkVersion 21才能使用

    只需在构建普通Notification的构建器builder上添加对bigContentView属性设置为RemoteView(自定义的通知样式),如需要对通知展开视图RemoteView里的UI控件设置监听,需要通过设置广播和RemoteView的setOnClickPendingIntent()方法配合使用

    Notification notification;
    NotificationManager manager;
    static Receive receive;
    //发送自定义视图的通知
    public void sendNotification(){
    Notification.Builder builder=new Notification.Builder(this);
    builder.setSmallIcon(R.drawable.ic_launcher_background); //设置小图标(未展开上方显示图标)
    builder.setTicker("QlynMusic又出新歌了,快来收听吧"); //显示最先在顶部收到的通知
    builder.setWhen(System.currentTimeMillis()); //发送时间
    builder.setAutoCancel(false);//点击通知后是否取消通知
    builder.setWhen(System.currentTimeMillis());//设置通知的时间
    //setVisibility用来设置通知在什么情况下会显示
    /*
    Notification.VISIBILITY_PUBLIC:任何情况都会显示通知。
    Notification.VISIBILITY_PRIVATE:只有在没有锁屏时会显示通知。
    Notification.VISIBILITY_SECRET:在pin、password等安全锁和没有锁屏的情况下才能够显示通知。
    */
    builder.setVisibility( Notification.VISIBILITY_PUBLIC );

    //设置通知响应的事件Activity,PendingIntent可以理解为延迟的Intent
    Intent intent =new Intent (this,MainActivity.class);
    PendingIntent pendingIntent =PendingIntent.getActivity(this, 0, intent,0);
    builder.setContentIntent(pendingIntent);

    //设置通知振动:下标为偶数表示静止时长,奇数为振动时长,单位为毫秒
    //需在注册地方声明权限:
    //<uses-permission android:name="android.permission.VIBRATE" />
    long[] vibrates= {0,1000,1000,1000};//此时立即振动,然后静止一秒,再振动一次
    builder.setVibrate(vibrates);

    //构建一条通知Notification
    notification=builder.build();
    //获得自定义视图
    remoteViews=new RemoteViews( getPackageName(),R.layout.information );
    //设置remote里的显示图片
    remoteViews.setImageViewResource( R.id.nAlbum, android.R.drawable.btn_star_big_on);
    remoteViews.setImageViewResource( R.id.nAppIcon, R.mipmap.ic_launcher);
    remoteViews.setImageViewResource( R.id.nLastSong, android.R.drawable.ic_media_previous);
    if(mediaPlayer.isPlaying()){
    remoteViews.setImageViewResource( R.id.nPlay, R.drawable.pause);
    }else{
    remoteViews.setImageViewResource( R.id.nPlay, R.drawable.play);
    }
    remoteViews.setImageViewResource( R.id.nNextSong, android.R.drawable.ic_media_ff);

    //设置RemoteViews里的控件监听,与广播接收器配合使用。
    intent = new Intent("play");//设置intent的Action(http://www.amjmh.com/v/)
    PendingIntent pIntentPause = PendingIntent.getBroadcast(this, 0,
    intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.nPlay, pIntentPause);
    intent = new Intent("lastSong");//设置intent的Action
    pIntentPause = PendingIntent.getBroadcast(this, 0,
    intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.nLastSong, pIntentPause);
    intent = new Intent("nastSong");//设置intent的Action
    pIntentPause = PendingIntent.getBroadcast(this, 0,
    intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.nNextSong, pIntentPause);

    //将自定义视图传入通知的展开视图
    notification.bigContentView=remoteViews;
    //发送通知
    manager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE );
    manager.notify( 1,notification );
    }
    ————————————————

  • 相关阅读:
    PHP 函数
    PHP 循环
    PHP 数组
    PHP Switch 语句
    PHP If...Else 语句
    PHP 字符串
    PHP 变量
    用where导致group by分组字段的索引失效
    mysql之filesort原理
    Windows定时开机并开启工作软件
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11414312.html
Copyright © 2011-2022 走看看