android自定义通知栏并通过广播实现监听
16lz
2021-01-23
用于实现类似音乐播放器通知栏功能(播放、暂停、上下一曲)
1、自定义通知栏
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default"); builder.setSmallIcon(android.R.drawable.sym_def_app_icon); RemoteViews rv = new RemoteViews(getPackageName(), R.layout.message); rv.setTextViewText(R.id.tv, "泡沫");//修改自定义View中的歌名 //修改自定义View中的图片(两种方法) rv.setImageViewResource(R.id.iv, R.mipmap.ic_launcher); rv.setImageViewBitmap(R.id.iv, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); builder.setContent(rv);//设置button监听为发送广播------------------ Intent previous = new Intent("com.example.Bean.MyBroadcastReceiver"); PendingIntent pi_previous = PendingIntent.getBroadcast(Tongzhi.this, 0, previous, PendingIntent.FLAG_UPDATE_CURRENT); rv.setOnClickPendingIntent(R.id.btn1, pi_previous);//---------------------------------------- Notification notification = builder.build(); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0x1, notification);
<?xml version="1.0" encoding="utf-8"?>
2、自定义广播
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "okgb", Toast.LENGTH_SHORT).show(); }}
................
发送这条广播测试广播是否注册成功
Intent integer = new Intent("com.example.Bean.MyBroadcastReceiver"); sendBroadcast(integer);
更多相关文章
- Android之MVC——Model通知View去更新(实用)
- Android通知Header详解
- 26. android Notification 状态栏通知
- Android中添加常驻通知栏
- Android Notification 通知封装成工具类
- Android 自定义通知Notification 适配不同背景颜色
- Android通知的使用及设置
- Android之RemoteViews篇上————通知栏和桌面小控件
- Android通知MediaScanner扫描指定的文件