Android项目仿UC浏览器和360手机卫士消息常驻栏(通知栏)
程序员文章站
2024-03-03 22:02:04
之前网上看了下自定义消息栏,通知栏,了解到了notification这个控件,发现uc浏览器等都是这种类型,今天写个demo实现下,如图:
其中每个按钮都有不同的功能...
之前网上看了下自定义消息栏,通知栏,了解到了notification这个控件,发现uc浏览器等都是这种类型,今天写个demo实现下,如图:
其中每个按钮都有不同的功能,代码如下:
package com.example.textwsjdemo; import android.app.activity; import android.app.notification; import android.app.notificationmanager; import android.app.pendingintent; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.os.bundle; import android.view.keyevent; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.remoteviews; import android.widget.toast; public class mainactivity extends activity { private button bt_hehe; private notificationmanager notificationmanager; private notification notification; private int icon; private charsequence tickertext; private long when; remoteviews contentview; private intent intent; private pendingintent pendingintent; private int notification_id = 0; private mybroadcast receiver; private static string action = "a"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); receiver = new mybroadcast(); intentfilter filter = new intentfilter(); filter.addaction("a"); filter.addaction("b"); filter.addaction("c"); filter.addaction("d"); registerreceiver(receiver, filter); initview(); initdata(); } private void initdata() { icon = r.drawable.ic_launcher; // 通知图标 tickertext = "hello"; // 状态栏显示的通知文本提示 when = system.currenttimemillis(); // 通知产生的时间,会在通知信息里显示 } private void initview() { bt_hehe = (button) findviewbyid(r.id.bt_hehe); bt_hehe.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub // 启动提示栏 createnotification(); } }); } private void createnotification() { notificationmanager = (notificationmanager) getsystemservice(context.notification_service); notification = new notification(); notification.icon = icon; notification.tickertext = tickertext; notification.when = when; /*** * 在这里我们用自定的view来显示notification */ contentview = new remoteviews(getpackagename(), r.layout.notification_item); contentview.settextviewtext(r.id.text11, "小说"); contentview.settextviewtext(r.id.text22, "视频"); contentview.settextviewtext(r.id.text33, "新闻"); contentview.settextviewtext(r.id.text44, "扯淡"); // contentview.settextviewtext(r.id.notificationpercent, "0%"); // contentview.setprogressbar(r.id.notificationprogress, 100, 0, false); // //进度条 // contentview.setimageviewresource(r.id.image,r.drawable.more_advice); // //加载图片 // contentview.setimageviewresource(r.id.image,r.drawable.more_attention); // contentview.setimageviewresource(r.id.image,r.drawable.more_evaluate); // contentview.setimageviewresource(r.id.image,r.drawable.more_about); // contentview.settextviewtext(r.id.text,"hello,this message is in a custom expanded view"); // //文本 notification.flags = notification.flag_ongoing_event; // 设置常驻,不能滑动取消 //默认跳转的主界面 intent = new intent(this, mainactivity.class); intent.addflags(intent.flag_activity_single_top); pendingintent = pendingintent.getactivity(this, 0, intent, 0); //自定义跳转 contentview.setonclickpendingintent(r.id.ll_11, pendingintent.getbroadcast(mainactivity.this, 11, new intent().setaction("a"), pendingintent.flag_update_current)); contentview.setonclickpendingintent(r.id.ll_22, pendingintent.getbroadcast(mainactivity.this, 11, new intent().setaction("b"), pendingintent.flag_update_current)); contentview.setonclickpendingintent(r.id.ll_33, pendingintent.getbroadcast(mainactivity.this, 11, new intent().setaction("c"), pendingintent.flag_update_current)); contentview.setonclickpendingintent(r.id.ll_44, pendingintent.getbroadcast(mainactivity.this, 11, new intent().setaction("d"), pendingintent.flag_update_current)); notification.contentview = contentview; notification.contentintent = pendingintent; notificationmanager.notify(notification_id, notification); } // 取消通知 private void cancelnotification() { notificationmanager.cancelall(); } @override protected void ondestroy() { cancelnotification(); unregisterreceiver(receiver); } @override public boolean onkeydown(int keycode, keyevent event) { if ((keycode == keyevent.keycode_back)) { system.out.println("按下了back键 onkeydown()"); cancelnotification(); } return super.onkeydown(keycode, event); } class mybroadcast extends broadcastreceiver { @override public void onreceive(context context, intent intent) { if(intent.getaction().equals("a")){ toast.maketext(mainactivity.this, "11111111111111", toast.length_long).show(); startactivity(new intent(mainactivity.this, activitytext1.class)); } if(intent.getaction().equals("b")){ toast.maketext(mainactivity.this, "222222222222222", toast.length_long).show(); startactivity(new intent(mainactivity.this, activitytext2.class)); } if(intent.getaction().equals("c")){ toast.maketext(mainactivity.this, "333333333333", toast.length_long).show(); startactivity(new intent(mainactivity.this, activitytext3.class)); } if(intent.getaction().equals("d")){ toast.maketext(mainactivity.this, "4444444444444", toast.length_long).show(); startactivity(new intent(mainactivity.this, activitytext4.class)); } } } }
以下是一些属性的设置:
/* * 添加声音 * notification.defaults |=notification.default_sound; * 或者使用以下几种方式 * notification.sound = uri.parse("file:///sdcard/notification/ringer.mp3"); * notification.sound = uri.withappendedpath(audio.media.internal_content_uri, "6"); * 如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"flag_insistent" * 如果notification的defaults字段包括了"default_sound"属性,则这个属性将覆盖sound字段中定义的声音 */ /* * 添加振动 * notification.defaults |= notification.default_vibrate; * 或者可以定义自己的振动模式: * long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒 * notification.vibrate = vibrate; * long数组可以定义成想要的任何长度 * 如果notification的defaults字段包括了"default_vibrate",则这个属性将覆盖vibrate字段中定义的振动 */ /* * 添加led灯提醒 * notification.defaults |= notification.default_lights; * 或者可以自己的led提醒模式: * notification.ledargb = 0xff00ff00; * notification.ledonms = 300; //亮的时间 * notification.ledoffms = 1000; //灭的时间 * notification.flags |= notification.flag_show_lights; */ /* * 更多的特征属性 * notification.flags |= flag_auto_cancel; //在通知栏上点击此通知后自动清除此通知 * notification.flags |= flag_insistent; //重复发出声音,直到用户响应此通知 * notification.flags |= flag_ongoing_event; //将此通知放到通知栏的"ongoing"即"正在运行"组中 * notification.flags |= flag_no_clear; //表明在点击了通知栏中的"清除通知"后,此通知不清除, * //经常与flag_ongoing_event一起使用 * notification.number = 1; //number字段表示此通知代表的当前事件数量,它将覆盖在状态栏图标的顶部 * //如果要使用此字段,必须从1开始 * notification.iconlevel = ; //
最后附上源码:源码下载
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。