我们在QQ项目中实现了通知栏后台运行,以及来新消息提示,通常在消息通知时,我们经常用到两个组件Toast和Notification。特别是重要的和需要长时间显示的信息,用Notification就最合适不过了。当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态栏,就可以看到通知信息了,Android这一创新性的UI组件赢得了用户的一致好评,就连苹果也开始模仿了。其实有点类似于Windows的托盘显示。
下面我们就来根据QQ小项目,来具体分析一下。先看下两张效果图:
一、通知栏的布局文件,在我们这个QQ小项目中,当我们在好友列表的Activity按返回键的时候,先作一个程序进入后台运行的标记(可以是全局变量,也可以保存到SharedPreferenced文件中),然后发送一个广播,我们通过在服务里接收这个广播,就马上初始化后台运行的通知栏的view,当新消息到来时,我们就不把消息通过广播发送出去了(因为没有Activity在运行),而是直接通过更新通知栏来提醒用户,同时发送一个通知(带声音、带振动)。下面是我们这个在通知栏的view的布局文件notify_view.xml:
-
<?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical">
-
-
<LinearLayout
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:orientation="vertical"
-
android:padding="2dp">
-
-
<RelativeLayout
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content">
-
-
<ImageView
-
android:id="@+id/notify_imageLog"
-
android:layout_width="40dp"
-
android:layout_height="40dp"
-
android:layout_alignParentLeft="true"
-
android:layout_centerVertical="true"
-
android:paddingLeft="5dp"
-
android:src="@drawable/h001"/>
-
-
<TextView
-
android:id="@+id/notify_name"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_centerVertical="true"
-
android:layout_toRightOf="@+id/notify_imageLog"
-
android:paddingLeft="5dp"
-
android:text="name"
-
android:textColor="#000000"
-
android:textSize="20sp"/>
-
</RelativeLayout>
-
-
<LinearLayout
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_gravity="center"
-
android:orientation="horizontal">
-
-
-
<TextView
-
android:id="@+id/notify_msg"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_weight="1"
-
android:paddingLeft="15dp"
-
android:text="msg"
-
android:textColor="@color/black"
-
android:textSize="15sp"/>
-
-
<TextView
-
android:id="@+id/notify_time"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_weight="1"
-
android:gravity="right"
-
android:paddingRight="15dp"
-
android:text="time"
-
android:textColor="@color/black"
-
android:textSize="15sp"/>
-
</LinearLayout>
-
</LinearLayout>
-
-
</LinearLayout>
二、初始化通知栏view的方法,在GetMsgService中写一个方法,初始化我们这个通知栏的view:
-
-
-
-
privatevoidsetMsgNotification(){
-
inticon=R.drawable.notify;
-
CharSequencetickerText="";
-
longwhen=System.currentTimeMillis();
-
mNotification=newNotification(icon,tickerText,when);
-
-
-
mNotification.flags=Notification.FLAG_ONGOING_EVENT;
-
-
RemoteViewscontentView=newRemoteViews(mContext.getPackageName(),
-
R.layout.notify_view);
-
contentView.setTextViewText(R.id.notify_name,util.getName());
-
contentView.setTextViewText(R.id.notify_msg,"手机QQ正在后台运行");
-
contentView.setTextViewText(R.id.notify_time,MyDate.getDate());
-
-
mNotification.contentView=contentView;
-
-
Intentintent=newIntent(this,FriendListActivity.class);
-
PendingIntentcontentIntent=PendingIntent.getActivity(mContext,0,
-
intent,PendingIntent.FLAG_UPDATE_CURRENT);
-
-
mNotification.contentIntent=contentIntent;
-
mNotificationManager.notify(Constants.NOTIFY_ID,mNotification);
-
}
三,好友列表Activity返回按键的广播接收者,用户按返回键发送广播,并做好标记,程序进入后台运行:
-
-
privateBroadcastReceiverbackKeyReceiver=newBroadcastReceiver(){
-
-
@Override
-
publicvoidonReceive(Contextcontext,Intentintent){
-
-
Toast.makeText(context,"QQ进入后台运行",0).show();
-
setMsgNotification();
-
}
-
};
四,通过handler更新通知栏,我们是通过handler来处理消息并更新通知栏的:
-
-
privateHandlerhandler=newHandler(){
-
publicvoidhandleMessage(Messagemsg){
-
switch(msg.what){
-
caseMSG:
-
intnewMsgNum=application.getNewMsgNum();
-
newMsgNum++;
-
application.setNewMsgNum(newMsgNum);
-
TranObject<TextMessage>textObject=(TranObject<TextMessage>)msg
-
.getData().getSerializable("msg");
-
-
if(textObject!=null){
-
intform=textObject.getFromUser();
-
Stringcontent=textObject.getObject().getMessage();
-
-
ChatMsgEntityentity=newChatMsgEntity("",
-
MyDate.getDateEN(),content,-1,true);
-
messageDB.saveMsg(form,entity);
-
-
-
inticon=R.drawable.notify_newmessage;
-
CharSequencetickerText=form+":"+content;
-
longwhen=System.currentTimeMillis();
-
mNotification=newNotification(icon,tickerText,when);
-
-
mNotification.flags=Notification.FLAG_NO_CLEAR;
-
-
mNotification.defaults|=Notification.DEFAULT_SOUND;
-
-
mNotification.defaults|=Notification.DEFAULT_VIBRATE;
-
mNotification.contentView=null;
-
-
Intentintent=newIntent(mContext,
-
FriendListActivity.class);
-
PendingIntentcontentIntent=PendingIntent.getActivity(
-
mContext,0,intent,0);
-
mNotification.setLatestEventInfo(mContext,util.getName()
-
+"("+newMsgNum+"条新消息)",content,
-
contentIntent);
-
}
-
mNotificationManager.notify(Constants.NOTIFY_ID,mNotification);
-
break;
-
-
default:
-
break;
-
}
-
}
-
};
四,监听消息,我们监听收消息线程中收到的消息先判断程序是否运行在后台,如果在后台,我们就直接把消息发送给handler,如果不是,就通过广播发送出去这个消息,所以:我们首先需要在按返回键的进入后台的时候,做一个标记,表示程序进入后台运行,我这里是通过保存在SharedPreferenced文件中的,其实可以保存到应用的全局变量:
-
in.setMessageListener(newMessageListener(){
-
-
@Override
-
publicvoidMessage(TranObjectmsg){
-
-
if(util.getIsStart()){
-
if(msg.getType()==TranObjectType.MESSAGE){
-
-
-
Messagemessage=handler.obtainMessage();
-
message.what=MSG;
-
message.getData().putSerializable("msg",msg);
-
handler.sendMessage(message);
-
}
-
}else{
-
IntentbroadCast=newIntent();
-
broadCast.setAction(Constants.ACTION);
-
broadCast.putExtra(Constants.MSGKEY,msg);
-
sendBroadcast(broadCast);
-
}
-
}
-
});
-
转自http://blog.csdn.net/way_ping_li/article/details/8054416
分享到:
相关推荐
Android 实现自定义通知栏,并不断更新通知栏的进度条,模拟下载效果。 注:只是模拟下载进度,没有下载功能。
Android仿QQ消息通知栏新消息实时更新效果,实时接收新消息显示并更新,点击通知栏清除所有新消息
Android实现Notification的通知栏常驻.zip
android NotificationListenerService 监听通知栏,android NotificationListenerService 监听通知栏 android NotificationListenerService 监听通知栏
Android开发之获取通知栏的内容更多查看我博客:https://blog.csdn.net/xiayiye5/article/details/83688396
Android下载,在通知栏更新进度
通知栏内更新,下载完成自动安装并清除消息通知,已修改测试,可直接用于项目
Anroid,一个简单的带按钮自定义通知栏,欢迎下载试用,人人为我,我为人人,共同学习,共同进步
通知栏接受消息跳转到相应的界面的处理
模拟通知栏提示下载进度,这是一个学习源码。如果想做一个升级下载功能,然后在通知栏里面显示进度,这将是一个非常不错的选择。
Android一个带有进度条的通知栏的DEMO,对初学Android的初学者是一个不错的参考例子,代码实现了点击显示按钮就会在通知栏上出现一个有图片和进度条的提示,点击取消按钮就会取消通知栏的提示通知。
这几天做一个小软件在API28(Android 9.0)的模拟器上测试时,发现通知栏无效,经过一番查询,了解到:API26(Android 8.0)以后,引入了**通知渠道(Notification Channels)**这么一个东西来帮助用户管理通知。...
修改android 2.3版本Notification通知栏不显示的Bug Android 服务端下载 Notification通知栏 断点下载
android对通知栏的操作 利用service而不是notifymanager 在程序崩溃或强制退出的时候通知栏图标能够主动消失
Android 使用Notification自定义通知栏显示。自定义系统状态栏通知类NotificationExtend,也可以手动更新状态栏。依据此项目可以做成线程接受,或者广播接受自动更新。我博客网址: ...
Android实现沉浸式通知栏-通知栏与导航栏颜色相同
android消息通知栏Notification
有些是模拟的,代码也比较乱,有需求的抽着看吧 现在就测试了一下能用,没整理
拆出来的,自己新手弄了好久,也添加了些备注,希望对新手有帮助 思路是 1)创建自定义视图 2)获取远程视图对象(注:Notification的contentView不能为空) 3)设置PendingIntent(来响应各种事件) ...
本demo实现了android 后台更新软件,单独启动服务下载,完全可以用于项目。