使用Notification发送消息通知
2016-05-22 11:44
344 查看
demo代码:
MainActivity.java
package com.example.notificationdemo;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
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.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
NotificationManager manager;//通知控制类
int notification_ID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
findViewById(R.id.btn_send).setOnClickListener(this);
findViewById(R.id.btn_cancle).setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_send:
sendNotification();
break;
case R.id.btn_cancle:
manager.cancel(notification_ID);
break;
}
}
/**
* 构造notification并发送到通知栏
*/
private void sendNotification(){
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("我来自NotificationDemo");//设置通知内容
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.build();//4.1以上
Notification notification = builder.getNotification();//4.1以下
manager.notify(notification_ID, notification);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.notificationdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="12" android:targetSdkVersion="18" /> <span style="color:#ff0000;"><uses-permission android:name="android.permission.FLASHLIGHT"/></span> <span style="color:#ff0000;"><uses-permission android:name="android.permission.VIBRATE"/></span> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.notificationdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
相关文章推荐
- 【POJ 1286】Necklace of Beads(Polya 定理)
- Android中的布局属性
- 死磕Tomcat7源码之二:web组件初始化
- Spring MVC学习笔记——服务器端验证
- JAVA之使用JDBC连接数据库-使用微软sqljdbc连接SQL Server 2005数据库
- 机器学习经典书籍
- Linux系统上的文件类型及一些基本命令使用
- C++模板
- R语言基本数据类型和一些一本函数
- 寻找下一个结点
- 如何有效拦截和过滤垃圾评论
- 06-图3 六度空间
- cp: omitting directory”错误的解释和解决办法
- Java集合框架:ArrayList、LinkedList、HashSet、TreeSet、HashMap、Iterator
- 正在相菜鸟进发,修炼中......java程序访问Sqlite数据库
- GCC编译使用方法
- mechanize
- python正则表达式
- UNIX网络编程笔记(2)—套接字编程简介
- 随机数