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 ...
随机推荐
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.1
Show that the inner product $$\bex \sef{x_1\wedge \cdots \wedge x_k,y_1\wedge \cdots\wedge y_k} \eex ...
- WPF 中资源路径的问题
WPF 中资源路径的问题 1. 引用当前工程的资源(注意xxxx.png的build action 应设置为Resource 或Embedded Resource) <ImageBrush Im ...
- jQuery mobile 核心功能
原文地址:http://jquerymobile.com/demos/1.0b2/#/demos/1.0b2/docs/about/features.html 基于 jQuery 核心,使用和jQue ...
- 深入浅出JavaScript函数 v 0.5
本文的观点是建立在<JavaScript权威指南 6th Ed> <JavaScript高级编程 3th Ed> <JavaScript精粹 2th Ed>之上, ...
- Linux内核,文件系统移植过程中出现的一些问题与解决办法
1.bootm地址和load address一样 此种情况下,bootm不会对uImage header后的zImage进行memory move的动作,而会直接go到entry point开始执行. ...
- Javascript and DOM学习
1.输出 document.write.(); 2.alert警告 3.confirm消息对话框:当用户点击"确定"按钮时,返回true,取消时,返回false 4.prompt提 ...
- leetcode@ [36/37] Valid Sudoku / Sudoku Solver
https://leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puz ...
- 【noip模拟】考试总结
今天睡了14个小时啊 把一星期的觉都补回来了 要不是被叫醒了 我肯定还在睡觉- - 其实现在还想睡... 集训真是伤身啊 感觉再睡就要睡成sb了 鉴于昨天被完虐(真·完虐 怒垫底) 来写篇总结 得分: ...
- ActivityNotFoundException: No Activity found to handle Intent
代码如下: Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction ...
- mac linux netstat -n
一直都不明白为什么mac就是不现实8080端口,现在明白了 USERtekiiMac-3:~ user$ netstat -an | grep tcp46 tcp46 0 0 *.8009 *.* L ...