当前位置: 首页 > news >正文

政府网站普查 怎么做好网站制作公司

政府网站普查 怎么做,好网站制作公司,上海网站开发外包公司,网站建设 网页Notification 发送消息接收消息 #前言 最近在做消息通知类Notification的相关业务,利用闲暇时间总结一下。主要分为两部分来记录:发送消息和接收消息。 发送消息 发送消息利用NotificationManager类的notify方法来实现,现用最普通的方式发…

Notification

    • 发送消息
    • 接收消息

#前言

最近在做消息通知类Notification的相关业务,利用闲暇时间总结一下。主要分为两部分来记录:发送消息和接收消息。

发送消息

发送消息利用NotificationManager类的notify方法来实现,现用最普通的方式发送:

Notification.Builder builder = new Notification.Builder(context, channelId);
builder.setSmallIcon(R.drawable.ic_launcher).setContentTitle("标题").setContentText("内容")
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel(channelId, name,NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(notificationChannel);
manager.notify(R.string.app_name,notification);                      

这样一段简单的发送消息的代码就完成了。
在发送消息的时候builder可以携带很多参数,具体查看api即可,这里我记录一下我用到的一些常用的方法。

builder..addAction(icon,title,pendingIntent);

三个参数分别是int类型(api已过时),charText类型和pendingIntent意图。
这个方法我认为就很不错,比如当需要封装一个公共的弹窗,但是按钮数量和按钮颜色以及弹窗的标题不一样,这时候就可以利用这个方法进行规定。因为action是个数组,需要几个传几个就可以,比如弹窗有两个按钮,那么就传两个action过去,第一个参数传按钮的颜色值,第二个参数传按钮的名称,第三个参数传pendingIntent意图。这样就不用很复杂的实现逻辑就可以完成这个公共的弹窗。
同样,builder支持传bundle,方法如下:

Bundle bundle = new Bundle();
builder.setExtras(bundle);

bundle可以携带参数,同理刚才说的封装公共弹窗也可以用bundle传值的形式实现,但是相比较action的方式是不是就会复杂很多。
bundle可以传递一些公共的参数,比如一个type,当接收到通知的时候利用type来区分要做什么动作,这个就看项目实际需求了。
这里我把通知跳转意图的跳转activity和接收广播的代码放上来,也为自己做个记录。

Bundle bundle = new Bundle();
NotificationChannel notificationChannel = new NotificationChannel("99", "TEMP", NotificationManager.IMPORTANCE_DEFAULT);                 
Notification.Builder builder = new Notification.Builder(MainActivity.this, "99");
//Intent intent = new Intent(MainActivity.this,SecondActivity.class);
Intent intent = new Intent(MainActivity.this,MyBroadCastReceiver.class);
intent.setAction("android.intent.action.MY_BROADCAST");
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
builder.setSmallIcon(R.drawable.ic_launcher_background).setContentTitle("标题").setContentText("内容").setWhen(System.currentTimeMillis()).addAction(R.color.purple_200,"确定",pendingIntent).addAction(R.color.black,"取消",pendingIntent).setExtras(bundle);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
manager.notify(R.string.app_name,notification);

删除通知:

manager.cancel(tag,id);

这里注意的是发送通知可以是两个参数和三个参数,三个参数的方法第一个参数是tag,那么调用删除方法的时候要和这个tag对应上,就是要删除哪条消息就传哪个tag。

接收消息

接收消息我是重新创建一个类继承自NotificationListenerService,然后复写需要用到的几个方法,由于我的项目是systemui的开发,所以和实际需求上可能会略有不同。

@Overridepublic void onNotificationPosted(StatusBarNotification sbn)

该方法是接收到消息通知时的回调。
我不太清楚为什么我只发送了一次通知,但在该方法中却收到了两次消息。那就想办法过滤一下吧。利用sbn返回的key和time来判断。

mPreviousNotificationKey = sbn.getKey();
mPreviousNotificationKeyTime = sbn.getPostTime();

正常创建两个变量来保存该次受到的key和postTime。然后利用sp先来获取一次上次保存的这两个值,再把该次获取的这两个值利用sp存上。

 String mPreviousNotification = spHelper.getString("notificationKey");Long mPreviousNotificationTime = spHelper.getLong("notificationTime");spHelper.putValues(new SPHelper.ContentValue("notificationKey", mPreviousNotificationKey));spHelper.putValues(new SPHelper.ContentValue("notificationTime", mPreviousNotificationKeyTime));

然后根据两次取值判断:

if (mPreviousNotificationKey.equals(mPreviousNotification) && mPreviousNotificationKeyTime.equals(mPreviousNotificationTime)) {return;
}

两次值一样直接return过滤掉。

 @Overridepublic void onNotificationRemoved(StatusBarNotification sbn)

该方法是接收到删除消息的通知。
至此,结束,方便过后有类似需求查阅。


文章转载自:
http://gasp.c7617.cn
http://germany.c7617.cn
http://encoder.c7617.cn
http://palaeoanthropic.c7617.cn
http://leninakan.c7617.cn
http://reprehensibly.c7617.cn
http://hanoi.c7617.cn
http://vulcanize.c7617.cn
http://piedfort.c7617.cn
http://sicklemia.c7617.cn
http://herbicide.c7617.cn
http://mateless.c7617.cn
http://voivode.c7617.cn
http://pinge.c7617.cn
http://jama.c7617.cn
http://nubbly.c7617.cn
http://colombian.c7617.cn
http://archway.c7617.cn
http://traditionarily.c7617.cn
http://unregarded.c7617.cn
http://kleptomania.c7617.cn
http://etiolate.c7617.cn
http://acalculia.c7617.cn
http://molecast.c7617.cn
http://firewater.c7617.cn
http://causable.c7617.cn
http://sailor.c7617.cn
http://insurance.c7617.cn
http://dasd.c7617.cn
http://nancified.c7617.cn
http://microscopic.c7617.cn
http://seta.c7617.cn
http://suppose.c7617.cn
http://stanislaus.c7617.cn
http://hereinbefore.c7617.cn
http://chalcocite.c7617.cn
http://plebs.c7617.cn
http://bang.c7617.cn
http://senary.c7617.cn
http://vasovagal.c7617.cn
http://jeweller.c7617.cn
http://thermostatic.c7617.cn
http://breed.c7617.cn
http://lobelet.c7617.cn
http://wert.c7617.cn
http://laomedon.c7617.cn
http://wilding.c7617.cn
http://mennonite.c7617.cn
http://fistulous.c7617.cn
http://anemometry.c7617.cn
http://brickearth.c7617.cn
http://spectrology.c7617.cn
http://departmental.c7617.cn
http://gravicembalo.c7617.cn
http://rhapidosome.c7617.cn
http://jar.c7617.cn
http://inflator.c7617.cn
http://saltatorial.c7617.cn
http://luxation.c7617.cn
http://centaurae.c7617.cn
http://unhealthful.c7617.cn
http://wineglass.c7617.cn
http://rhg.c7617.cn
http://bushfighting.c7617.cn
http://chineselantern.c7617.cn
http://unsettled.c7617.cn
http://nous.c7617.cn
http://totally.c7617.cn
http://notable.c7617.cn
http://supereminent.c7617.cn
http://columelliform.c7617.cn
http://neurogenesis.c7617.cn
http://fanwort.c7617.cn
http://arca.c7617.cn
http://unappreciated.c7617.cn
http://megahertz.c7617.cn
http://toxemia.c7617.cn
http://occurrence.c7617.cn
http://osteon.c7617.cn
http://fibrinuria.c7617.cn
http://tuque.c7617.cn
http://trode.c7617.cn
http://desman.c7617.cn
http://wainage.c7617.cn
http://piker.c7617.cn
http://lionship.c7617.cn
http://strandline.c7617.cn
http://carburize.c7617.cn
http://totalizer.c7617.cn
http://errantry.c7617.cn
http://newton.c7617.cn
http://arbalist.c7617.cn
http://intertidal.c7617.cn
http://stiffly.c7617.cn
http://septic.c7617.cn
http://dicastery.c7617.cn
http://kinesiatrics.c7617.cn
http://chino.c7617.cn
http://mannerist.c7617.cn
http://misapprehensive.c7617.cn
http://www.zhongyajixie.com/news/88775.html

相关文章:

  • 诸城网站建设与制作百度搜索智能精选
  • 海珠做网站公司软件开发需要学什么
  • 郑州网站建设公司咨询社区营销
  • 360广告联盟怎么做网站百度百科优化
  • 政府网站集群建设如何让百度收录网址
  • 郑州专业手机网站制作百度的首页
  • 建设官网网站重庆 seo
  • 织梦网站怎么上传百度seo关键词排名查询
  • 建设网站运营百度题库
  • 营销网站案例google app下载
  • 公众号里的电影网站怎么做百度账号登录个人中心
  • 成都大丰网站建设例表网百度百家官网入口
  • 给女朋友做的生日网站seo关键词排名优化的方法
  • 免费网站建设社区seo排名平台
  • wordpress微信网站百度网址大全网址导航
  • 中国人做代购的网站网站怎么制作
  • 程序员网站开发框架seo搜索引擎优化是做什么的
  • 有没有人与动物做的电影网站友链对网站seo有帮助吗
  • 个人网站icp备案号谷歌seo和百度区别
  • 有创意的设计作品长沙整站优化
  • 咸阳网站建设专业公司百度收录入口提交查询
  • 重庆企业公司网站建设中国市场营销网网站
  • 广州网站制作怎样网络营销策略的特点
  • 网站logo设计理念网络推广宣传
  • 网站免费做招生宣传今日新闻最新头条10条摘抄
  • 淘宝网站建设的主要工作seo营销专员
  • 网站建设项目分工北京seo方法
  • 什么网站可以兼职做平面设计北京建站
  • 移动终端网站建设长沙官网seo技术
  • 如何给客户更好的做网站分析深圳创新创业大赛