Android_Notification
xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.notificationdemo.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/send_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="send Message"/>
<Button
android:id="@+id/cancel_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="cancel Message"
/>
</LinearLayout>
</RelativeLayout>
源代码:
package com.example.notificationdemo; import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; /**
* Notification是显示在手机状态栏的消息(手机状态栏位于手机最顶端),代表一种全局效果的通知 通知栏的内容:图标,标题,内容,时间,点击后响应
* 如何实现通知栏: 获取notificationManager 显示通知栏:notify(id,notification) 取消通知栏:cancel(id)
* 构造Notification并设置显示内容 通知栏通知可以设置声音提示,指示灯以及震动效果,后两者注意需添加权限
*
*
* @author Administrator
*
*/
public class MainActivity extends Activity implements OnClickListener { private Button send;
private Button cancel;
private NotificationManager manager;
private int NOTIFICATION_ID; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button) findViewById(R.id.send_btn);
cancel = (Button) findViewById(R.id.cancel_btn);
send.setOnClickListener(this);
cancel.setOnClickListener(this);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//系统常用服务
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.send_btn:
sendNotification();
break;
case R.id.cancel_btn:
manager.cancel(NOTIFICATION_ID);//取消发送的通知
break;
}
} /**
* 构造notification并发送到通知栏
*/
private void sendNotification() {
// TODO Auto-generated method stub
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0);
Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);// 设置图标
builder.setTicker("hello");// 设置手机状态栏的提示
// 拖拽开手机状态栏提示的内容
builder.setWhen(System.currentTimeMillis());// 设置时间
builder.setContentTitle("通知");// 设置标题
builder.setContentText("内容");// 设置内容
builder.setContentIntent(pintent);// 设置点击之后的响应,意图 /*
* builder.setDefaults(Notification.DEFAULT_SOUND);//设置声音
* builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯
* builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动
*/
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.getNotification();// 4.1以下
// builder.build();//4.1及以上
manager.notify(NOTIFICATION_ID, notification);//发送通知到通知栏
} }
Android_Notification的更多相关文章
- Android 通知之 Notification
Notifications | Android Developershttp://developer.android.com/guide/topics/ui/notifiers/notificatio ...
- Android Notification使用方法
1.http://www.cnblogs.com/plokmju/p/android_Notification.html 2.http://blog.csdn.net/vipzjyno1/articl ...
随机推荐
- Swift不可变数组
Objective-C编写了2个不同的类来区分不可变数组(NSArray)和可变数组(NSMutableArray): Swift通过使用常量和变量来区分不可变数组和可变数组. 只要将数组定义为常量, ...
- 刑事案件的构成要素 zt
论刑事案件的构成要素 马忠红 2013-03-22 14:05:33 来源:<中国人民公安大学学报:社会科学版>(京)2012年5期 [内容提要]刑事案件是由诸多要素构成的一个系 统. ...
- Ofbiz初探
转:http://xmmartin.blog.51cto.com/2310947/771236 主导建设一个电子商务系统希望从Ofbiz了解中获得一些借鉴1.下载ofbiz,目前的版本是10.04,下 ...
- 怎么解决div覆盖内容却没覆盖的问题?
一.在上下结构的div布局中,可能出现div覆盖div,但是内容却没有出现覆盖的现象.看看一个示例 1: <!DOCTYPE html> 2: <html> 3: <he ...
- 微信浏览器内置JavaScript 对象:WeixinJSBridge
微信公众平台开发 微信公众平台开发模式 企业微信公众平台 微信浏览器 分享到朋友圈 发送给好友 分享到腾讯微博 作者:方倍工作室 原文: http://www.cnblogs.com/txw1958/ ...
- bzoj 2716 天使玩偶(CDQ分治,BIT)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=29234 [题意] 询问当前点与已知点的最小曼哈顿距离. [思路 ...
- Cordova CrossWalk
http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-2-cordovabarcodescanner/ 军训结束后,同学们 ...
- Morris Traversal
昨天临近要睡觉的时候做了一个leetcode题目,"Recover BST",算法很容易就想到了,直接找出两个异常点就好了,但是我写的算法是用栈实现的非递归遍历,空间复杂度是O(N ...
- leetcode—sum root to leaf number
题目如下: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a ...
- openstack 在线repo
https://repos.fedorapeople.org/repos/openstack/openstack-kilo/