android通知栏怎么添加控件

android通知栏怎么添加控件,第1张

Notification的自定义布局是RemoteViews,和其他RemoteViews一样,在自定义视图布局文件中,仅支持FrameLayout、LinearLayout、RelativeLayout三种布局控件和AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper这些显示控件,不支持这些类的子类或Android提供的其他控件。否则会引起ClassNotFoundException异常

步骤如下:

1)创建自定义视图

2)获取远程视图对象(注:Notification的contentView不能为空)

3)设置PendingIntent(来响应各种事件)

4)发起Notification

大体4步骤这里就不详细说了,下面就把DEMO中的列子拿出来说下

样式:

1.自定义带按钮通知栏(如下样式)

正在进行的

“正在进行的”通知使用户了解正在运行的后台进程。例如,音乐播放器可以显示正在播放的音乐。也可以用来显示需要长时间处理的 *** 作,例如下载或编码视频。“正在进行的”通知不能被手动删除。

实现方法如下:

实现方法如下:

/**

 * 带按钮的通知栏

 */

public void showButtonNotify(){

NotificationCompat.Builder mBuilder = new Builder(this)

RemoteViews mRemoteViews = new RemoteViews(getPackageName(), R.layout.view_custom_button)

mRemoteViews.setImageViewResource(R.id.custom_song_icon, R.drawable.sing_icon)

//API3.0 以上的时候显示按钮,否则消失

mRemoteViews.setTextViewText(R.id.tv_custom_song_singer, "周杰伦")

mRemoteViews.setTextViewText(R.id.tv_custom_song_name, "七里香")

//如果版本号低于(3。0),那么不显示按钮

if(BaseTools.getSystemVersion() <= 9){

mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.GONE)

}else{

mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.VISIBLE)

}

//

if(isPlay){

mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_pause)

}else{

mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_play)

}

//点击的事件处理

Intent buttonIntent = new Intent(ACTION_BUTTON)

/* 上一首按钮 */

buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PREV_ID)

//这里加了广播,所及INTENT的必须用getBroadcast方法

PendingIntent intent_prev = PendingIntent.getBroadcast(this, 1, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT)

mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_prev, intent_prev)

/* 播放/暂停  按钮 */

buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID)

PendingIntent intent_paly = PendingIntent.getBroadcast(this, 2, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT)

mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_play, intent_paly)

/* 下一首 按钮  */

buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_NEXT_ID)

PendingIntent intent_next = PendingIntent.getBroadcast(this, 3, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT)

mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_next, intent_next)

mBuilder.setContent(mRemoteViews)

.setContentIntent(getDefalutIntent(Notification.FLAG_ONGOING_EVENT))

.setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示

.setTicker("正在播放")

.setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级

.setOngoing(true)

.setSmallIcon(R.drawable.sing_icon)

Notification notify = mBuilder.build()

notify.flags = Notification.FLAG_ONGOING_EVENT

mNotificationManager.notify(notifyId, notify)

}

如果您对回答满意,请关注一下俺的微博

//注册按钮广播

private void setButtonBroadCast(){

final String STATUS_BAR_COVER_CLICK_ACTION="download"

getDownNotification().contentView.setViewVisibility(R.id.downloadCancle,View.VISIBLE)

BroadcastReceiver onClickReceiver = new BroadcastReceiver() {

private boolean flag = false

@Override

public void onReceive(Context context, Intent intent) {

if (intent.getAction().equals(STATUS_BAR_COVER_CLICK_ACTION)) {

//在这里处理点击事件

interceptFlag= true

//取消通知栏

}

}}

IntentFilter filter = new IntentFilter()

filter.addAction(STATUS_BAR_COVER_CLICK_ACTION)

mContext.registerReceiver(onClickReceiver, filter)

Intent buttonIntent = new Intent(STATUS_BAR_COVER_CLICK_ACTION)

PendingIntent pendButtonIntent = PendingIntent.getBroadcast(mContext, 0, buttonIntent, 0)

getDownNotification().contentView.setOnClickPendingIntent(R.id.downloadCancle, pendButtonIntent)

//R.id.trackname为你要监听按钮的id

// mRemoteViews.setOnClickPendingIntent(R.id.trackname, pendButtonIntent)

}

)

小米手机的闹钟在菜单上的时钟里面就可以设置。具体 *** 作如下:

1、首先在小米手机上找到“时钟”的图标,点击后打开进入时钟页面。

2、接下来在打开的时间页面中点击“闹钟”标签,然后点击右下角的“添加”按钮。

3、这时就会打开手机的设置闹钟页面,在这里先设置好闹钟的时间。

4、接下来找到“响铃后删除此闹钟”一项,把其后面的开关设置为开5、这样该闹钟就设置好了,同时出现在闹钟列表中,只是该闹钟会在响铃后自动删除。6、不过大家需要注意的是,如果你手机设置了自动开关机,需要设置关机也要响闹钟哦。点击闹钟页面右上角的“设置”按钮。7、在打开的下拉菜单中占击“设置”菜单项。8、这时会打开手机的闹钟设置页面,点击“关机闹钟”一项,把其后面的开关设置为开即可。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11913829.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存