0703-APP-Notification-statue-bar
1.展示显示textTicker和仅仅有icon的两种情况:当參数showTicker为true时显示否则不显示
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(textId); // choose the ticker text
String tickerText = showTicker ? getString(textId) : null; // Set the icon, scrolling text and timestamp
Notification notification = new Notification(moodId, tickerText,
System.currentTimeMillis()); // Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title),
text, makeMoodIntent(moodId)); // Send the notification.
// We use a layout id because it is a unique number. We use it later to cancel.
mNotificationManager.notify(R.layout.status_bar_notifications, notification);
2.展示通过view创建notifaction
// Instead of the normal constructor, we're going to use the one with no
// args and fill
// in all of the data ourselves. The normal one uses the default layout
// for notifications.
// You probably want that in most cases, but if you want to do something
// custom, you
// can set the contentView field to your own RemoteViews object.
Notification notif = new Notification(); // This is who should be launched if the user selects our notification.
notif.contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationDisplay.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra("moodimg", moodId), PendingIntent.FLAG_UPDATE_CURRENT); // In this sample, we'll use the same text for the ticker and the
// expanded notification
CharSequence text = getText(textId);
notif.tickerText = text; // the icon for the status bar
notif.icon = moodId; // our custom view
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.status_bar_balloon);
contentView.setTextViewText(R.id.text, text);
contentView.setImageViewResource(R.id.icon, moodId);
notif.contentView = contentView; // we use a string id because is a unique number. we use it later to
// cancel the
// notification
mNotificationManager.notify(R.layout.status_bar_notifications, notif);
3.notifacation 设置声音和震动
private void setDefault(int defaults) {
// This method sets the defaults on the notification before posting it.
// This is who should be launched if the user selects our notification.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, StatusBarNotifications.class), 0);
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.status_bar_notifications_happy_message);
final Notification notification = new Notification(
R.drawable.stat_happy, // the icon for the status bar
text, // the text to display in the ticker
System.currentTimeMillis()); // the timestamp for the notification
notification.setLatestEventInfo(
this, // the context to use
getText(R.string.status_bar_notifications_mood_title),
// the title for the notification
text, // the details to display in the notification
contentIntent); // the contentIntent (see above)
notification.defaults = defaults;
mNotificationManager.notify(
R.layout.status_bar_notifications, // we use a string id because it is a unique
// number. we use it later to cancel the
notification); // notification
}
公共代码(被调用的代码)
private PendingIntent makeMoodIntent(int moodId) {
// The PendingIntent to launch our activity if the user selects this
// notification. Note the use of FLAG_UPDATE_CURRENT so that if there
// is already an active matching pending intent, we will update its
// extras to be the ones passed in here.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, NotificationDisplay.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("moodimg", moodId),
PendingIntent.FLAG_UPDATE_CURRENT);
return contentIntent;
}
4.常驻通知的样例
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package com.example.android.apis.app; import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; // Need the following import to get access to the app resources, since this
// class is in a sub-package.
import com.example.android.apis.R; /**
* This is an example of implementing an application service that can
* run in the "foreground". It shows how to code this to work well by using
* the improved Android 2.0 APIs when available and otherwise falling back
* to the original APIs. Yes: you can take this exact code, compile it
* against the Android 2.0 SDK, and it will against everything down to
* Android 1.0.
*/
public class ForegroundService extends Service {
static final String ACTION_FOREGROUND = "com.example.android.apis.FOREGROUND";
static final String ACTION_BACKGROUND = "com.example.android.apis.BACKGROUND"; private static final Class[] mStartForegroundSignature = new Class[] {
int.class, Notification.class};
private static final Class[] mStopForegroundSignature = new Class[] {
boolean.class}; private NotificationManager mNM;
private Method mStartForeground;
private Method mStopForeground;
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1]; /**
* This is a wrapper around the new startForeground method, using the older
* APIs if it is not available.
*/
void startForegroundCompat(int id, Notification notification) {
// If we have the new startForeground API, then use it.
if (mStartForeground != null) {
mStartForegroundArgs[0] = Integer.valueOf(id);
mStartForegroundArgs[1] = notification;
try {
mStartForeground.invoke(this, mStartForegroundArgs);
} catch (InvocationTargetException e) {
// Should not happen.
Log.w("ApiDemos", "Unable to invoke startForeground", e);
} catch (IllegalAccessException e) {
// Should not happen.
Log.w("ApiDemos", "Unable to invoke startForeground", e);
}
return;
} // Fall back on the old API.
setForeground(true);
mNM.notify(id, notification);
} /**
* This is a wrapper around the new stopForeground method, using the older
* APIs if it is not available.
*/
void stopForegroundCompat(int id) {
// If we have the new stopForeground API, then use it.
if (mStopForeground != null) {
mStopForegroundArgs[0] = Boolean.TRUE;
try {
mStopForeground.invoke(this, mStopForegroundArgs);
} catch (InvocationTargetException e) {
// Should not happen.
Log.w("ApiDemos", "Unable to invoke stopForeground", e);
} catch (IllegalAccessException e) {
// Should not happen.
Log.w("ApiDemos", "Unable to invoke stopForeground", e);
}
return;
} // Fall back on the old API. Note to cancel BEFORE changing the
// foreground state, since we could be killed at that point.
mNM.cancel(id);
setForeground(false);
} @Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
try {
com.example.android.apis.Log.log(getClass().getName()); mStartForeground = getClass().getMethod("startForeground",
mStartForegroundSignature);
com.example.android.apis.Log.log(mStartForeground.getName());
mStopForeground = getClass().getMethod("stopForeground",
mStopForegroundSignature);
} catch (NoSuchMethodException e) {
// Running on an older platform.
mStartForeground = mStopForeground = null;
}
} @Override
public void onDestroy() {
// Make sure our notification is gone.
stopForegroundCompat(R.string.foreground_service_started);
} // This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
handleCommand(intent);
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
} void handleCommand(Intent intent) {
if (ACTION_FOREGROUND.equals(intent.getAction())) {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.foreground_service_started); // Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.stat_sample, text,
System.currentTimeMillis()); // The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Controller.class), 0); // Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.local_service_label),
text, contentIntent); startForegroundCompat(R.string.foreground_service_started, notification); } else if (ACTION_BACKGROUND.equals(intent.getAction())) {
stopForegroundCompat(R.string.foreground_service_started);
}
} @Override
public IBinder onBind(Intent intent) {
return null;
} // ---------------------------------------------------------------------- /**
* <p>Example of explicitly starting and stopping the {@link ForegroundService}.
*
* <p>Note that this is implemented as an inner class only keep the sample
* all together; typically this code would appear in some separate class.
*/
public static class Controller extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.foreground_service_controller); // Watch for button clicks.
Button button = (Button)findViewById(R.id.start_foreground);
button.setOnClickListener(mForegroundListener);
button = (Button)findViewById(R.id.start_background);
button.setOnClickListener(mBackgroundListener);
button = (Button)findViewById(R.id.stop);
button.setOnClickListener(mStopListener);
} private OnClickListener mForegroundListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ForegroundService.ACTION_FOREGROUND);
intent.setClass(Controller.this, ForegroundService.class);
startService(intent);
}
}; private OnClickListener mBackgroundListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ForegroundService.ACTION_BACKGROUND);
intent.setClass(Controller.this, ForegroundService.class);
startService(intent);
}
}; private OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
stopService(new Intent(Controller.this,
ForegroundService.class));
}
};
}
}
凝视notification.flags能够设置notification能否够点击消除,是否自己主动消除等状态
0703-APP-Notification-statue-bar的更多相关文章
- 【转】can't find referenced method 'android.app.RemoteInput[] getRemoteInputs()' in class android.app.Notification$Action
原文网址:http://stackoverflow.com/questions/25508735/cant-find-referenced-method-android-app-remoteinput ...
- 安卓状态栏通知Status Bar Notification
安卓系统通知用户三种方式: 1.Toast Notification 2.Dialog Notification 3.Status Bar Notification Status Bar Notifi ...
- Android之Notification介绍
Notification就是在桌面的状态通知栏.这主要涉及三个主要类: Notification:设置通知的各个属性. NotificationManager:负责发送通知和取消通知 Notifica ...
- android学习笔记22——Notification
Notification ==> Notification是显示在手机状态栏的消息,位于手机屏幕的最上方: 一般显示手机当前网络.电池状态.时间等: Notification所代表的是一种全局效 ...
- Android之Notification的多种用法(转)
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们也 ...
- android Notification定义与应用
首先要明白一个概念: Intent 与 PendingIntent 的区别: Intent:是意图,即告诉系统我要干什么,然后做Intent应该做的事,而intent是消息的内容 PendingInt ...
- Android之Notification的多种用法
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们也 ...
- Android的状态栏通知(Notification)
通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息. 1.Layout布局文件: <RelativeLayout xmlns:an ...
- Notification的功能与用法
Notification是显示在手机状态的通知——手机状态栏位于手机屏幕的最上方,那里一般显示了手机当前的网络状态.时间等.Notification所代表的是一种具有全局效果的通知,程序一般通过Not ...
- android之Notification通知
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. pac ...
随机推荐
- 在Visual Studio 2010中使用DSL Tool特定领域开发 开篇
本来是很想写关于VS的DSL的文章的,有点小忙,就一直在拖延,忽然有看见了"<在Visual Studio 2012中使用VMSDK开发特定领域语言>",又有写的欲望了 ...
- BZOJ1954: Pku3764 The xor-longest Path
题解: 在树上i到j的异或和可以直接转化为i到根的异或和^j到根的异或和. 所以我们把每个点到根的异或和处理出来放到trie里面,再把每个点放进去跑一遍即可. 代码: #include<cstd ...
- sql null值
SQL Server 提供 SET CONCAT_NULL_YIELDS_NULL { ON | OFF } 来控制 null 与其它字符串连接的行为. 当 SET CONCAT_NULL_YIELD ...
- Sping表达式语言--SpEL
Spring表达式语言---SpEL 是一个支持运行时查询和操作对象的强大的表达式语言 语法类似于EL:SpEL使用#{...}作为定界符,所有在大括号中的字符都将被认为是SpEL SpEL为bean ...
- ListView属性
1. 背景色: listView设置背景色android:background="@drawable/bg",拖动或者点击list空白位置的时候发现ListItem都变成黑色. 因 ...
- 将android中的sample例子到eclipse中
SDK中带有很多的例子,那么我们怎么样导入到eclipse中呢?方法很简单,如下: 1. 新建android工程,选择Create project from existing sample, 2. 选 ...
- [Everyday Mathematics]20150103
试求极限$$\bex \vlm{n} \sez{\int_1^{e^2}\sex{\frac{\ln x}{x}}^n\rd x}^\frac{1}{n}.\eex$$
- 理解Android的手势识别
对于触摸屏,其原生的消息无非按下.抬起.移动这几种,我们只需要简单重载onTouch或者设置触摸侦听器setOnTouchListener即可进行处理.不过,为了提高我们的APP的用户体验,有时候我们 ...
- svn版本控制-windows篇
一.准备工作 1.获取 Subversion 服务器程序(服务端) 到官方网站(http://subversion.tigris.org/)下载最新的服务器安装程序.目前最新的是1.5版本,具体下载地 ...
- 解决:Eclipse导入工程后全是错误,连基本类型都不识别
当初在大学没时间完成作业时,总是喜欢网上搜一个或者拷贝同学的一个工程过来,导入到Eclipse中却全是红叉,连基本类型都不识别. 当时就纳闷了,难道是天要亡我之心不死?后来慢慢了解了,其实是导入的工程 ...