android 发音乐通知到通知栏

我们以音乐播放器为例子做一个通知到 通知栏(并且我们要求点击按钮切换通知栏按钮状态并做切歌的操作)
我们先写一个广播

public class XMPlayerReceiver extends BroadcastReceiver {
    public static final String PLAY_PRE = "play_pre";
    public static final String PLAY_NEXT = "play_next";
    public static final String PLAY_PAUSE = "play_pause";
    public static final String PLAY_PLAY = "play_play";
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(PLAY_NEXT)){//PLAY_NEXT
            Log.e("XMPlayerReceiver", "通知栏点击了下一首");
        }
        if (intent.getAction().equals(PLAY_PRE)) {;
            Log.e("XMPlayerReceiver", "通知栏点击了上一首");
        }
        if (intent.getAction().equals(PLAY_PAUSE)) {
            Log.e("XMPlayerReceiver", "通知栏点击了暂停");
        }
        if (intent.getAction().equals(PLAY_PLAY)) {
            Log.e("XMPlayerReceiver", "通知栏点击了开始");
        }
    }
}

AndroidManifest.xml中注册上我们写的广播

        <receiver
            android:name=".app.audioheler.ability.XMPlayerReceiver"
            android:exported="true"
            android:process=":player">
            <intent-filter>
                <action android:name="play_pre" />
                <action android:name="play_next" />
                <action android:name="play_pause" />
                <action android:name="play_play" />
            </intent-filter>
        </receiver>

准备好广播之后我们发一个推送到通知栏。

先自定义一个RemoteViews ,xml自己写

private RemoteViews initNotifyView(Bitmap bitmap) {
        String packageName = context.getPackageName();
        RemoteViews remoteView = new RemoteViews(packageName, R.layout.你的xml);
        remoteView.setImageViewBitmap(R.id.xml中放图的imageview id, Bitmap位图);
        remoteView.setTextViewText(R.id.xml中标题的id, "标题内容");
        remoteView.setTextViewText(R.id.xml中小标题的id, "小标题内容");

        Intent prv = new Intent(context,XMPlayerReceiver.class);//播放上一首
        prv.setAction(PLAY_PRE);
        PendingIntent intent_prev = PendingIntent.getBroadcast(context, 1, prv,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteView.setOnClickPendingIntent(R.id.上一首按钮的id, intent_prev);


        Intent next = new Intent(context,XMPlayerReceiver.class);//播放下一首
        next.setAction(PLAY_NEXT);
        PendingIntent intent_next = PendingIntent.getBroadcast(context, 2, next,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteView.setOnClickPendingIntent(R.id.下一首按钮的id, intent_next);


        Intent startpause = new Intent(context,XMPlayerReceiver.class);//暂停
        startpause.setAction(PLAY_PAUSE);
        PendingIntent intent_pause = PendingIntent.getBroadcast(context, 3, startpause,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteView.setOnClickPendingIntent(R.id.暂停按钮的id, intent_pause);

        Intent startplay = new Intent(context,XMPlayerReceiver.class);//播放
        startplay.setAction(PLAY_PLAY);
        PendingIntent intent_play = PendingIntent.getBroadcast(context, 4, startplay,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteView.setOnClickPendingIntent(R.id.播放按钮的id, intent_play);
        return remoteView;
    }

现在把自定义的样式发到通知栏上

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    String channelId = "notification";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//适配一下高版本
        NotificationChannel channel = new NotificationChannel(channelId,
                "listen",
                NotificationManager.IMPORTANCE_DEFAULT);
        channel.enableLights(false); //是否在桌面icon展示小红点
        channel.setLightColor(Color.RED); //小红点颜色
        channel.setSound(null, null);//关了通知默认提示音
        channel.setShowBadge(false); //是否在久按桌面图标时显示此渠道的通知
        notificationManager.createNotificationChannel(channel);
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.mipmap.ic_launcher)//这玩意在通知栏上显示一个logo
            .setCategory(CATEGORY_MESSAGE)
            .setDefaults(DEFAULT_ALL)
            .setOngoing(true);
    //点击通知栏跳转的activity
    Intent intent = new Intent(context, NotifyActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
    PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setAutoCancel(false);//点击不让消失
    builder.setSound(null);//关了通知默认提示音
    builder.setPriority(PRIORITY_MAX);//咱们通知很重要
    builder.setVibrate(null);//关了车震
    builder.setContentIntent(pendingIntent);//整个点击跳转activity安排上
    builder.setOnlyAlertOnce(false);
    RemoteViews remoteViews = initNotifyView(bitmap);
    builder.setContent(remoteViews);//把自定义view放上
    builder.setCustomBigContentView(remoteViews);//把自定义view放上
    Notification notification = builder.build();
    notification.flags |= FLAG_ONGOING_EVENT;
    notification.flags |= Notification.FLAG_NO_CLEAR;//不让手动清除 通知栏常驻
    notification.sound = null;//关了通知默认提示音
    notificationManager.notify(notifyId, notification);

需要的时候调用上段代码将通知发到通知栏上  在BroadcastReceiver 中接收点击事件。若更新通知栏的样式,重新创建RemoteViews再发一遍通知好了  notifyId使用同一个。

当我们不需要这个通知栏播放器时  直接关闭通知

notificationManager.cancel(notifyId);

这俩biang玩意别跟别的app重了  带上自己包名

 

如有不重发通知更新通知栏的方法请告诉我

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值