Android的我怎么能拦截状态栏通知栏和状态栏

Android自定义状态栏通知(Status Notification)的正确实现_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Android自定义状态栏通知(Status Notification)的正确实现
来源:Linux社区&
作者:ace1985
在应用开发中,经常会使用到状态栏通知(Status Notification),例如新浪微博、网易新闻等提供的推送消息,软件后台更新时进度的显示等等,如下图所示:
看了网上很多关于Notification的博客文章,发现几乎没有一个能将自定义状态栏通知完全实现正确的,因此,本文就来说说实现自定义状态栏通知经常被忽略的一些知识点。
1) 使用Notification最常见的场景
运行在后台的Service当需要和用户交互时,由于它不能直接启动一个Activity,因此,Service必须使用Notification来间接的启动Activity(当用户点击Notification时跳转)。
2) 自定义布局文件支持的控件类型
Notification的自定义布局是RemoteViews,因此,它仅支持FrameLayout、LinearLayout、RelativeLayout三种布局控件,同时支持AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper这些UI控件。对于其他不支持的控件,使用时将会抛出ClassNotFoundException异常。
3) Notification支持的Intent类型(都是PendingIntent类的实例)
contentIntent:在通知窗口区域,Notification被单击时的响应事件由该intent触发;
deleteIntent:在通知窗口区域,当用户点击全部清除按钮时,响应该清除事件的Intent;
fullScreenIntent:响应紧急状态的全屏事件(例如来电事件),也就是说通知来的时候,跳过在通知区域点击通知这一步,直接执行fullScreenIntent代表的事件。上面三种PendingIntent可以拉起Activity、Service和BroadcastReceiver,如图所示:
相关资讯 & & &
& (11/06/:26)
& (01/20/:15)
& (05/01/:29)
& (03/06/:38)
& (02/18/:27)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款2395人阅读
Android(55)
在点击通知之后跳转到Activity之前,我们可能希望做一些处理,这时可以使用广播。
NotificationActivity.java:package com.zzj.ui.notificationInterceptD
import android.app.A
import android.app.N
import android.app.Notification.B
import android.app.NotificationM
import android.app.PendingI
import android.content.I
import android.os.B
import android.view.V
import com.zzj.ui.R;
public class NotificationActivity extends Activity {
private NotificationManager notificationM// 通知管理器
private int notificationId = -1;
private int requestCode = 99;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_intercept_activity);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
public void send(View view) {
notificationId++;
requestCode++;
Intent intent = new Intent(this, NotificationBroadcast.class);
intent.putExtra(&requestCode&, requestCode);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.dog).setAutoCancel(true)
.setContentTitle(&消息&).setContentText(&消息& + notificationId)
.setContentIntent(pendingIntent);
Notification notification = builder.build();
notificationManager.notify(notificationId, notification);
这里的Intent是跳转到BroadcastReceiver,然后通过PendingIntent.getBroadcast()方法获取PendingIntent对象。
notification_intercept_activity.xml:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical& &
android:id=&@+id/to_activity1&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:onClick=&send&
android:text=&发送通知& /&
&/LinearLayout&广播类NotificationBroadcast:package com.zzj.ui.notificationInterceptD
import android.content.BroadcastR
import android.content.C
import android.content.I
import android.widget.T
public class NotificationBroadcast extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
int requestCode = intent.getIntExtra(&requestCode&, -1);
if (requestCode == -1) {
Toast.makeText(context, &未获取到requestCode!&, Toast.LENGTH_LONG)
Toast.makeText(context, &requestCode --& & + requestCode,
Toast.LENGTH_LONG).show();
}我们可以在onReceive方法中做相应的处理(例如将消息设为已读),然后根据不同的业务逻辑,跳转到相应的Activity。
注册广播:&receiver android:name=&com.zzj.ui.notificationInterceptDemo.NotificationBroadcast& &&/receiver&
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:882488次
积分:9499
积分:9499
排名:第1740名
原创:261篇
转载:130篇
评论:80条
(3)(4)(7)(7)(1)(1)(6)(10)(1)(2)(4)(1)(1)(1)(9)(6)(13)(15)(1)(6)(10)(7)(4)(7)(32)(3)(22)(29)(9)(8)(4)(16)(4)(24)(13)(4)(15)(34)(1)(1)(13)(6)(5)(4)(2)(8)(4)(5)您当前位置: >>
>> 浏览文章
【Android】状态栏通知Notification用法
Notification可
以在屏幕最顶部的状态栏上显示一个图标通知(QQ用的就是这个),通知的同时可以播放声音,以及振动提示用户,点击通知还可以返回指定的Activity.
今天例子的效果图:
下面我们来实现。
布局main.xml:
android=&/apk/res/android&
android:orientation=&vertical&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:id=&@+id/bt1&
android:layout_height=&wrap_content&
android:layout_width=&fill_parent&
android:text=&Notification测试&
android:id=&@+id/bt2&
android:layout_height=&wrap_content&
android:layout_width=&fill_parent&
android:text=&清除Notification&
就两个按钮,一个添加Notification,一个删除Notification
java代码: public class main extends Activity {
/** Called when the activity is first created. */
int notification_id=;
NotificationM
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nm=(NotificationManager)getSystemService(NOTIFICAT ION_SERVICE);
Button bt1=(Button)findViewById(R.id.bt1);
bt1.setOnClickListener(bt1lis);
Button bt2=(Button)findViewById(R.id.bt2);
bt2.setOnClickListener(bt2lis);
OnClickListener bt1lis=new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
//showNotification(R.drawable.home,&图标边的文字&,&标题&,&内容&);
nm.cancel(notification_id);
public void showNotification(int icon,String tickertext,String title,String content){
//设置一个唯一的ID,随便设置
//Notification管理器
Notification notification=new Notification(icon,tickertext,System.currentTimeMil lis());
//后面的参数分别是显示在顶部通知栏的小图标,小图标旁的文字(短暂显示,自动消失)系统当前时间(不明白这个有什么用)
notification.defaults=Notification.DEFAULT_ALL;
//这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND
//振动为Notification.DEFAULT_VIBRATE;
//Light为Notification.DEFAULT_LIGHTS,在我的Milestone上好像没什么反应
//全部为Notification.DEFAULT_ALL
//如果是振动或者全部,必须在AndroidManifest.xml加入振动权限
PendingIntent pt=PendingIntent.getActivity(this, 0, new Intent(this,main.class), 0);
//点击通知后的动作,这里是转回main 这个Acticity
notification.setLatestEventInfo(this,title,content ,pt);
nm.notify(notification_id, notification);
AndroidManifest.xml加入权限:
赞助商链接
中国设计在线网 All Rights Reserved.
互联网违法和不良信息举报
信息产业部备案号:湘ICP备号【04-05水贴】安卓6.0状态栏屏蔽通知如何恢复?【android吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:1,187,811贴子:
【04-05水贴】安卓6.0状态栏屏蔽通知如何恢复?收藏
求解求解 求大神帮忙按了小铃铛就没办法出现通知了
来网博优壹学习android 应用开发,先试听后报名,android 应用开发先就业后付款!android 应用开发来网博,企业定制培养,百分百满意就业.
来人啊啊啊啊啊
设置里自己找
同求 我也恢复不了
楼主现在问题解决了没有?我现也是这样,我都快急哭了……
android课程哪家好?android培训来上市公司博为峰培训,零基础到精通120天!android课程好学吗?android前景如何?来上市公司学软件开发,高薪就业!
设定 -应用程序- 应用程序信息 -通知
登录百度帐号推荐应用}

我要回帖

更多关于 cordova 状态栏通知 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信