android 通知栏处理

本文详细介绍了如何在Android中实现自定义的通知,包括变量初始化、构造Notification对象、显示通知及撤销通知的过程。通过具体的代码示例展示了如何设置通知的图标、标题、内容等,并使用RemoteViews定制通知视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

变量初始化:

mIntent = new Intent(this, GinwaveIMusic.class); mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0); //构造Notification对象 mNotification = new Notification(); mNotification.flags = Notification.FLAG_NO_CLEAR|Notification.FLAG_ONGOING_EVENT;; mNotification.icon = R.drawable.icon;
显示通知:

private void showNotification(){ mNotification.when = System.currentTimeMillis(); mNotification.tickerText = mPresentSong.Song_Title; initializeNotificationView( mPresentSong.Song_Title, mPresentSong.Song_Player + "-" + mPresentSong.Song_Album, R.drawable.icon); mNotification.contentIntent = mPendingIntent; //mNotification.setLatestEventInfo(this, mPresentSong.Song_Title, mPresentSong.Song_Player + "-" + mPresentSong.Song_Album, mPendingIntent); mNotificationManager.notify(0, mNotification); } private void initializeNotificationView(String title, String content, int notificationImage) { RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification); contentView.setImageViewResource(R.id.notification_image, notificationImage); contentView.setTextViewText(R.id.notification_text_tiele, title); contentView.setTextViewText(R.id.notification_text_content, content); mNotification.contentView = contentView; } 撤销通知:

private void dismissNotification(){ mNotificationManager.cancel(0); }
布局文件:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/notification_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/notification_text_tiele" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/notification_text_content" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>效果截图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值