Notification可以在屏幕最顶部的状态栏上显示一个图标通知,通知的同时可以播放声音,以及振动提示用户,点击通知还可以返回指定的Activity.
今天例子的效果图:

布局main.xml:

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Notification测试"
/>
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="清除Notification"
/>

java代码:
package com.pocketdigi.Notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class main extends Activity {
/** Called when the activity is first created. */
int notification_id=19172439;
NotificationManager nm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nm=(NotificationManager)getSystemService(NOTIFICATION_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(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showNotification(R.drawable.home,"图标边的文字","标题","内容");
}

};
OnClickListener bt2lis=new OnClickListener(){

@Override
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.currentTimeMillis());
//后面的参数分别是显示在顶部通知栏的小图标,小图标旁的文字(短暂显示,自动消失)系统当前时间(不明白这个有什么用)
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加入权限:

更多相关文章

  1. 拦截短信,不让系统发通知
  2. Android(安卓)Wear Preview- 归档通知(Stacking Notifications)
  3. android应用不生成桌面应用图标
  4. Android(安卓)Notification RemoteViews
  5. android 键盘 搜索键
  6. Android(安卓)5.0有哪些变化
  7. android自带的drawable图标ico名称对应表
  8. android中各种图标尺寸以及多分辨率支持方法
  9. android:versionCode和android:versionName 用途(转) App自动更

随机推荐

  1. android小说阅读源码、bilibili源码、MVP
  2. Android中设置activity的background为Wal
  3. android TextView属性详解
  4. Android(安卓)hardware so加载流程
  5. android 沉浸式主题记录
  6. Android月历控件(DatePicker)和时间控件(Tim
  7. android rom短信模块最后获取的Cursor字
  8. Android控件visible,invisible和gone
  9. Android
  10. 使用Vitamio打造自己的Android万能播放器