Android Notification 用法
来源:互联网 发布:淘宝在线人工客服时间 编辑:程序博客网 时间:2024/05/19 05:37
Notification可以在屏幕最顶部的状态栏上显示一个图标通知(QQ用的就是这个),通知的同时可以播放声音,以及振动提示用户,点击通知还可以返回指定的Activity.
今天例子的效果图:
下面我们来实现。
布局main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/bt1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Notification测试"
/>
<Button android:id="@+id/bt2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="清除Notification"
/>
</LinearLayout>
就两个按钮,一个添加Notification,一个删除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);
}
}
12
<uses-permission android:name="android.permission.VIBRATE" /> <!-- 允许振动 -->
- Android Notification 用法
- android notification 的用法
- android notification的用法
- Android Notification用法
- Android基础-Notification用法
- Android通知Notification用法
- Android Notification 用法
- android Notification简单用法
- Android中Notification的用法
- Android的Notification的用法
- android sdk --- Notification用法实例
- android app notification的用法
- Android Notification的多种用法
- android中Notification的用法
- android通知栏Notification用法
- Android Notification的多种用法
- Android Notification.setLatestEventInfo弃用和Notification.Builder用法
- Android Notification.setLatestEventInfo弃用和Notification.Builder用法
- Dom解析
- oracle 中 for update与for update of的区别
- DLX hdu3111 hdu 3909 hdu3663 hdu3957
- 关于js兼容性的解决办法
- Noip 2003T4 传染病控制
- Android Notification 用法
- 【工作相关】 工作初段 1
- android学习:ExpandableListView的运用
- struts2中action 跳转的传值问题
- 【iOS-Cocos2d游戏开发之八】开启高清(960*640)模式问题与解答、图片适配以及设置iphone横竖屏
- OceanBase:编译、安装和配置手册
- 跳槽切不能说的四大离职理由
- 访问动态链接库中的C++类和资源
- 淘宝自主研发的海量数据库OceanBase开源