StatusBar的使用_例子讲解
2012-04-12 13:00
239 查看
Notification是一般是在Service这种后台服务中发起的,当后台服务要通知用户某一事项时,就发起一个Notification,然后通过这个Notification启动相应的Activity,而Service自己不应该启动Activity.
生成Notification要用到两个类,一个是Notification,两一个是NotificationManager.其实不建议通过Notification的构造函数创建Notification,而是用Notification.Builder来帮助生成。
生成一个Notification的步骤:
1. 得到一个NotificationManger的引用。这个要通过getSystemService(NOTIFICATION_SERVICE)方法来得到.
2. 生成一个Notification对象:
3. 定义Notification的内容和PendingIntent
4. 将Notification传递给NotificationManager
以下为程序例子:在MainActivity中有一个按钮,当点击按钮后会产生一个Notification在status bar中,然后点击打开这个Notification会打开ActionNotification这个Activity.
MainActivity.java:
ActionNotification.java:
程序运行结果:
为Notification增加声音提醒:
增加系统默认声音:
notification.defaults |= Notification.DEFAULT_SOUND;
增加自定义声音:
notification.sound = Uri.parse("file:///sdcard/notification/ring.mp3");
为Notification增加震动提醒:
增加系统默认震动:
notification.defaults |= Notification.DEFAULT_VIBRATE;
增加自定义震动:
long[] vibrate = {0. 100. 200, 300};
notification.vibrate = vibrate;
如果需要使用震动功能,需要加入权限:<uses-permission android:name="android.permission.VIBRATE" />
为Notification加入灯闪提醒:
增加系统默认灯闪:
notification.defaults |= Notification.DEFAULT_LIGTHS;
增加自定义灯闪:
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
生成Notification要用到两个类,一个是Notification,两一个是NotificationManager.其实不建议通过Notification的构造函数创建Notification,而是用Notification.Builder来帮助生成。
生成一个Notification的步骤:
1. 得到一个NotificationManger的引用。这个要通过getSystemService(NOTIFICATION_SERVICE)方法来得到.
2. 生成一个Notification对象:
3. 定义Notification的内容和PendingIntent
4. 将Notification传递给NotificationManager
以下为程序例子:在MainActivity中有一个按钮,当点击按钮后会产生一个Notification在status bar中,然后点击打开这个Notification会打开ActionNotification这个Activity.
MainActivity.java:
public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.btn_notice); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { setNotification(); } }); } private void setNotification() { // 首先得到NotificationManager,通过getSystemService()方法得到 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 生成一个Notification,其实不建议直接采用构造方法,而是用Notification.Builder类生成Notification Notification notice = new Notification(R.drawable.close2, "have downloaded", System.currentTimeMillis()); // 定义Notification的信息和PendingIntent Intent intent = new Intent(this, ActionNotification.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); notice.setLatestEventInfo(this, "下载完成", "请打开查看", contentIntent); // 将Notification传递给NotificationManager nm.notify(1, notice); } }
ActionNotification.java:
public class ActionNotification extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textView = new TextView(this); textView.setText("歌曲 一千年以后 下载完成"); LinearLayout layout = new LinearLayout(this); layout.addView(textView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); setContentView(layout); } }
程序运行结果:
为Notification增加声音提醒:
增加系统默认声音:
notification.defaults |= Notification.DEFAULT_SOUND;
增加自定义声音:
notification.sound = Uri.parse("file:///sdcard/notification/ring.mp3");
为Notification增加震动提醒:
增加系统默认震动:
notification.defaults |= Notification.DEFAULT_VIBRATE;
增加自定义震动:
long[] vibrate = {0. 100. 200, 300};
notification.vibrate = vibrate;
如果需要使用震动功能,需要加入权限:<uses-permission android:name="android.permission.VIBRATE" />
为Notification加入灯闪提醒:
增加系统默认灯闪:
notification.defaults |= Notification.DEFAULT_LIGTHS;
增加自定义灯闪:
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
相关文章推荐
- jmock使用及例子讲解
- 使用数组元素倒置来讲解泛型的经典例子
- 昨天晚上学MFC的ADO,跟着书上的讲解和例子,完成了ADO的初级使用。
- android中service的使用方法-小例子讲解
- 关于matlab中princomp的使用说明讲解小例子
- apache mod_rewrite 模块使用的几个例子
- android 6.0 权限管理的学习资料和使用例子
- iOS7 StatusBar 使用小结
- [转]使用iPhone三轴感应器的代码例子
- iOS经典讲解之倒计时按钮JKCountDownButton的使用
- CListCtrl 使用演示的例子
- window.open的例子和使用方法以及参数说明(完整版)
- 使用C#获取网页HTML源码的例子
- [IrrLicht/中文/例子]IrrLicht 例子的中文讲解 - Tutorial 2: Quake 3 Map
- python3.x 的urllib使用例子
- gson使用在android使用例子
- Storm-源码分析- Multimethods使用例子
- 【Cocos2dX(2.x)_Lua开发之三】★重要必看篇★在Lua中使用自定义精灵(Lua脚本与自创建类之间的访问)及Lua基础讲解
- axis2+MyEclipse8.5实现webservice接口例子(没有使用插件)
- 广播接收机(监听网络例子,记录一下方便使用)