android服务- Notification创建通知 - startForeground 使用前台服务
2016-01-12 15:19
731 查看
版本3.0以上,推荐使用NotificationCompat.Builder类来构建Notification。
<span style="font-family:FangSong_GB2312;"> </span><span style="font-family:Courier New;"> @Override public void onCreate() { super.onCreate(); long[] vibrates = {0, 1000, 1000, 1000};</span>
<span style="font-family:Courier New;"> //使用前台服务 Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("This is title") .setContentText("This is content") .setContentIntent(pendingIntent) .setVibrate(vibrates) //震动 .setDefaults(1);//跟随手机设置 Notification notification = mBuilder.build(); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(1, notification); //当一个Service被当作Foreground来运行的时候,就不会因为内存不足而被销毁 startForeground(1, notification); Log.d("MyService", "onCreate executed"); }</span>
相关文章推荐
- android 中动态添加view
- Android中的so挂钩(hook)之Inline hook
- Android利用shape画虚线
- Android BLE蓝牙4.0开发详解
- android 自定义view(二)
- Android安全研究经验谈
- Android安全研究经验谈
- android中popupwindow的点滴
- 安卓开发之百度地图定位
- Fresco介绍 - 一个新的android图片加载库
- android选择图片拍照详解(裁剪-旋转-压缩)
- Android 判断一个url是否有效
- Android如何在java代码中设置margin
- Android Studio上NDK/JNI开发环境问题
- Android属性动画完全解析(三)-Interpolator和ViewPropertyAnimator的用法
- android studio 运行出现 com.android.ide.common.process.ProcessException
- Android 监听输入法隐藏与显示状态切换
- [Andorid开发艺术探索 读书笔记]View的事件体系(二)
- 解决更新Android SDK时出现A folder failed to be renamed or moved办法
- Android中定时执行任务的3种实现方法