Android编程之通知栏的用法小结

Android编程之通知栏的用法小结,第1张

概述本文实例总结了Android编程中通知栏的用法。分享给大家供大家参考,具体如下:

本文实例总结了AndroID编程中通知栏的用法。分享给大家供大家参考,具体如下:

很久没有使用AndroID的通知功能了,今天把两年前的代码搬出来一看,发现很多方法都废弃了,代码中各种删除线看的十分不爽。于是乎,打开Google,查看官方文档,学习最新的发送通知栏消息的方法。

本文中的代码均参照谷歌官方文档编写:

http://developer.androID.com/guIDe/topics/ui/notifIErs/notifications.HTML

1.首先,获取系统的通知服务:
复制代码 代码如下:notificationmanager nm = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);

2.发送一个最简单的通知

public voID simpleNotice(VIEw vIEw) {    //此Builder为androID.support.v4.app.NotificationCompat.Builder中的,下同。    Builder mBuilder = new Builder(this);    //系统收到通知时,通知栏上面显示的文字。    mBuilder.setTicker("天津,晴,2~15度,微风");    //显示在通知栏上的小图标    mBuilder.setSmallicon(R.drawable.consult_answer);    //通知标题    mBuilder.setContentTitle("天气预报");    //通知内容    mBuilder.setContentText("天津,晴,2~15度,微风");    //设置大图标,即通知条上左侧的图片(如果只设置了小图标,则此处会显示小图标)    mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.share_sina));    //显示在小图标左侧的数字    mBuilder.setNumber(6);    //设置为不可清除模式    mBuilder.setongoing(true);    //显示通知,ID必须不重复,否则新的通知会覆盖旧的通知(利用这一特性,可以对通知进行更新)    nm.notify(1,mBuilder.build());}

3.删除一个通知。参数即为通知的ID

nm.cancel(1);

4.发送一个通知,点击通知后跳转到一个Activity,从这个Activity返回后,进入程序内的某一个页面(一般为主页)

//点击通知进入一个Activity,点击返回时进入指定页面。public voID resultActivityBackApp(VIEw vIEw) {    Builder mBuilder = new Builder(this);    mBuilder.setTicker("通知标题2");    mBuilder.setSmallicon(R.drawable.ic_launcher);    mBuilder.setContentTitle("通知标题2");    mBuilder.setContentText("点击通知进入一个Activity,点击返回时进入指定页面。");    //设置点击一次后消失(如果没有点击事件,则该方法无效。)    mBuilder.setautoCancel(true);    //点击通知之后需要跳转的页面    Intent resultIntent = new Intent(this,ResultActivityBackApp.class);    //使用TaskStackBuilder为“通知页面”设置返回关系    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);    //为点击通知后打开的页面设定 返回 页面。(在manifest中指定)    stackBuilder.addParentStack(ResultActivityBackApp.class);    stackBuilder.addNextIntent(resultIntent);    PendingIntent pIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);    mBuilder.setContentIntent(pIntent);    // mID allows you to update the notification later on.    nm.notify(2,mBuilder.build());}

同时,需要在manifest中为点击通知后打开的Activity指定父Activity.

<activity  androID:name=".ResultActivityBackApp"  androID:parentActivityname=".MainActivity">  <Meta-data    androID:name="androID.support.PARENT_ACTIVITY"    androID:value=".MainActivity" /></activity>

(其中,activity的属性parentActivityname为API 16中的属性,Meta-data中的代码为兼容API 16以下。因此,对于大多数程序,这两个地方都得写。)

5.和上述4类似,只是在打开的Activity中返回时回到home页

//点击通知进入一个Activity,点击返回时回到桌面public voID resultActivityBackHome(VIEw vIEw) {    Builder mBuilder = new Builder(this);    mBuilder.setTicker("通知标题3");    mBuilder.setSmallicon(R.drawable.ic_launcher);    mBuilder.setContentTitle("通知标题3");    mBuilder.setContentText("点击通知进入一个Activity,点击返回时回到桌面");    //设置点击一次后消失(如果没有点击事件,则该方法无效。)    mBuilder.setautoCancel(true);    Intent notifyIntent = new Intent(this,ResultActivityBackHome.class);    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    PendingIntent pIntent = PendingIntent.getActivity(this,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);    mBuilder.setContentIntent(pIntent);    nm.notify(3,mBuilder.build());}

6.带进度条的通知

public voID progressNotice(VIEw vIEw) {    final Builder mBuilder = new Builder(this);    mBuilder.setTicker("通知标题4");    mBuilder.setContentTitle("Picture Download")        .setContentText("Download in progress")        .setSmallicon(R.drawable.ic_launcher);    // Start a lengthy operation in a background thread    new Thread(new Runnable() {      @OverrIDe      public voID run() {        int progress;        for (progress = 0; progress <= 100; progress++) {          // Sets the progress indicator to a max value,the current completion percentage,// and "determinate" state          mBuilder.setProgress(100,progress,false);          //不明确进度的进度条//          mBuilder.setProgress(0,true);          nm.notify(4,mBuilder.build());          // 模拟延时          try {            Thread.sleep(200);          } catch (InterruptedException e) {            e.printstacktrace();          }        }        // When the loop is finished,updates the notification        mBuilder.setContentText("Download complete");        // Removes the progress bar        mBuilder.setProgress(0,false);        nm.notify(4,mBuilder.build());      }    }    ).start();}

7.扩展布局的通知。按住通知条下滑,可以查看更详细的内容

public voID expandLayoutNotice(VIEw vIEw) {    Builder mBuilder = new Builder(this);    mBuilder.setTicker("通知标题5");    mBuilder.setSmallicon(R.drawable.ic_launcher);    mBuilder.setContentTitle("通知标题5");    mBuilder.setContentText("按住通知下拉可显示扩展布局");    NotificationCompat.InBoxStyle inBoxStyle = new NotificationCompat.InBoxStyle();    String[] events = new String[]{"Beijing","Tianjin","Shanghai","Guangzhou"};    // 设置扩展布局的标题    inBoxStyle.setBigContentTitle("Event tracker details:");    for (String s : events) {      inBoxStyle.addline(s);    }    mBuilder.setStyle(inBoxStyle);    nm.notify(5,mBuilder.build());}

8.自定义布局的通知栏。(根据谷歌的官方文档不推荐这么做,因为使用这种方式时,对不同屏幕进行适配需要考虑的因素太多。而且,通知栏应该展示的就是最简明扼要的信息,对于大多数程序默认的布局已经足够了。)

//自定义布局的通知public voID customLayoutNotice(VIEw vIEw) {    Builder mBuilder = new Builder(this);    mBuilder.setTicker("通知标题6");    mBuilder.setTicker("通知标题6");    mBuilder.setSmallicon(R.drawable.ic_launcher);    RemoteVIEws remoteVIEws = new RemoteVIEws(getPackagename(),R.layout.custom_layout_notice);    mBuilder.setContent(remoteVIEws);    //为RemoteVIEws上的按钮设置文字    remoteVIEws.setCharSequence(R.ID.custom_layout_button1,"setText","button1");    remoteVIEws.setCharSequence(R.ID.custom_layout_button2,"button2");    //为RemoteVIEws上的按钮设置点击事件    Intent intent1 = new Intent(this,CustomLayoutResultActivity.class);    intent1.putExtra("content","From button1 click!");    PendingIntent pIntentbutton1 = PendingIntent.getActivity(this,intent1,PendingIntent.FLAG_UPDATE_CURRENT);    remoteVIEws.setonClickPendingIntent(R.ID.custom_layout_button1,pIntentbutton1);    Intent intent2 = new Intent(this,CustomLayoutResultActivity.class);    intent2.putExtra("content","From button2 click!");    PendingIntent pIntentbutton2 = PendingIntent.getActivity(this,1,intent2,PendingIntent.FLAG_UPDATE_CURRENT);    remoteVIEws.setonClickPendingIntent(R.ID.custom_layout_button2,pIntentbutton2);    nm.notify(6,mBuilder.build());}

更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体 *** 作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家AndroID程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android编程之通知栏的用法小结全部内容,希望文章能够帮你解决Android编程之通知栏的用法小结所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

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

原文地址: http://outofmemory.cn/web/1147068.html

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

发表评论

登录后才能评论

评论列表(0条)

保存