android音频通知栏显示和隐藏以及通知栏不显示问题

需求

公司产品课程音频播放播放的需求,其中有一个通知栏呈现市场上网易云和QQ类似功能,可以显示对应内容和音乐播放状态切换,并且其中有一个防假学功能,对应在某个点对应提示有试题作答,并隐藏通知栏视图。

解决思路

1.音乐播放器状态和通知栏状态一致
播放器状态变更就是发送通知,进行状态进行刷新;状态栏控制控制播放器,可以通过广播通知播放器去控制音乐播放器状态;
2.隐藏通知栏
发送一个通知,进行通知栏隐藏

代码实现

音乐播放器状态变更发送通知,并对应通知栏的状态点击监听,关联对应的广播进行触发。

public void showNotify(PlayerStates states) {
        if (mContext == null) {
            return;
        }
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
        RemoteViews bigView = null;
        RemoteViews smallview = null;
        smallview = new RemoteViews(getPackageName(), R.layout.audio_notification);
        bigView = new RemoteViews(getPackageName(), R.layout.audio_notification_big);
        if (states == PlayerStates.PLAY) {
            smallview.setImageViewResource(R.id.play_btn, R.drawable.icon_play);
            bigView.setImageViewResource(R.id.play_btn, R.drawable.icon_play);
        } else {
            smallview.setImageViewResource(R.id.play_btn, R.drawable.icon_stop);
            bigView.setImageViewResource(R.id.play_btn, R.drawable.icon_stop);
        }
        SharedPreferencesUtil sharedPreferencesUtil = new SharedPreferencesUtil(getContext(), "notification");
        String courseName = sharedPreferencesUtil.getString("courseName", "");
        String teacher = sharedPreferencesUtil.getString("teacher", "");
        smallview.setTextViewText(R.id.title_one_name, courseName);
        bigView.setTextViewText(R.id.title_one_name, courseName);
        if (!teacher.equals("")) {
            smallview.setViewVisibility(R.id.title_two_name, VISIBLE);
            smallview.setTextViewText(R.id.title_two_name, "讲师:" + teacher);
            bigView.setTextViewText(R.id.title_two_name, "讲师:" + teacher);
        } else {
            smallview.setViewVisibility(R.id.title_two_name, GONE);
        }
        PendingIntent homeIntent = PendingIntent.getBroadcast(this.getContext(), 1, new Intent(NotificationBroadcast.ACTION), PendingIntent.FLAG_UPDATE_CURRENT);//动态广播注册
        bigView.setOnClickPendingIntent(R.id.play_btn, homeIntent);//设置通知栏监听,关联广播
        smallview.setOnClickPendingIntent(R.id.play_btn, homeIntent);
        mBuilder.setContent(smallview)//通知栏默认视图
                .setContentIntent(getDefalutIntent(Notification.FLAG_ONGOING_EVENT))
                .setWhen(System.currentTimeMillis())
                .setTicker("")
                .setOngoing(true)
                .setCustomBigContentView(bigView)//通知栏大视图
                .setSmallIcon(R.drawable.audio_background);
        Notification notify = mBuilder.build();
        notify.flags = Notification.FLAG_ONGOING_EVENT;
        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(200, notify);
    }

对应的广播接收者

public class NotificationBroadcast extends BroadcastReceiver {
    public static final String ACTION = "com.hb.vplayer.audio.NotificationBroadcast";
    private NotificationInterface mLs;
    public void setListener(NotificationInterface ls){
        this.mLs=ls;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        mLs.onPlayerClick();
    }
    interface NotificationInterface{
        public void onPlayerClick();
    }
}

对应在某个点对应提示有试题作答,并隐藏通知栏视图,主要通过发送通知,通知系统隐藏对应通知栏

public void hideNotifaction(String tag) {
        if (mContext == null) {
            return;
        }
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
        RemoteViews bigView = null;
        RemoteViews smallview = null;
        smallview = new RemoteViews(getPackageName(), com.hb.vplayer.R.layout.audio_notification);
        bigView = new RemoteViews(getPackageName(), com.hb.vplayer.R.layout.audio_notification_big);

        PendingIntent homeIntent = PendingIntent.getBroadcast(mContext, 1, new Intent(NotificationBroadcast.ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
        bigView.setOnClickPendingIntent(com.hb.vplayer.R.id.play_btn, homeIntent);
        smallview.setOnClickPendingIntent(com.hb.vplayer.R.id.play_btn, homeIntent);
        mBuilder.setContent(smallview)
                .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))
                .setWhen(System.currentTimeMillis())
                .setTicker("")
                .setOngoing(true)
                .setCustomBigContentView(bigView)
                .setSmallIcon(R.drawable.audio_background);
        ;
        Notification notify = mBuilder.build();
        notify.flags = Notification.FLAG_AUTO_CANCEL;//隐藏通知栏的flags的标记
        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(200, notify);
    }

遇到问题:

如果android的gradle的编译版本是26以及以上的,就要引入对应渠道,不然通知栏显示不出来。

 public void createNotificationChannel(){
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            NotificationManager mNotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            String id="default";
            CharSequence name="默认";
            String description="接收一通知";
            int importance= NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel=new NotificationChannel(id,name,importance);
            mChannel.setDescription(description);
            mChannel.enableLights(false);
            mChannel.enableVibration(false);
            mChannel.setVibrationPattern(new long[]{0});
            mChannel.setSound(null,null);
            mChannel.setLightColor(Color.RED);
            mNotificationManager.createNotificationChannel(mChannel);
        }
    }

对应在创建Notifcation要加入渠道进去

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, "default");   // gradle 的targetSdkVersion>=26  需要添加渠道

你可能感兴趣的:(android中级)