Android中,service的开启,默认是绑定activity的,是activity级的。

如果要实现当退出程序后,保持Service的运行,那么需要把service设置成为system级的,设置方法: 在AndroidManifest.xml中注册service时,加上属性android:process,如:
     

android:name = "com.jansun.pushnotification.PushNotificationService" android:enabled = "true" android:process = "system" />

     另外,还要在启动service时,加入FLAG_ACTIVITY_NEW_TASK标签,如:
     

public static void actionStart ( Context ctx ) { //System.out.println("---- Notification service started!"); Intent i = new Intent(ctx, PushNotificationService .class); i.setAction(ACTION_START); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ctx.startService(i); }


至此,当你退出程序后,service还在系统后台正常运行,目标达成。

 

更多相关文章

  1. android设置主题和自定义主题的方法
  2. Android(安卓)Studio 设置背景色
  3. Android(安卓)使用SVG
  4. Android应用开发——TextView控件属性列表
  5. android通过adb设置以太网共享
  6. Android:设置圆形头像,Android截取圆形图片
  7. 【安卓学习笔记】Android(安卓)Studio第9课——进度条ProgressBa
  8. Android(安卓)ListView 滑动背景为黑色的解决办法[转]
  9. Mac 下设置android NDK的环境

随机推荐

  1. 如果Imageview与Linearlayout有叠加且可
  2. Android的SDK与ADT不匹配问题
  3. android studio 报Error:failed to find
  4. 环境变量ANDROID_SDK_HOME的作用
  5. android控件之ImageButton android:scale
  6. android布局居中
  7. android:各种访问权限Permission
  8. android消息处理机制学习(一)-Handler,Mess
  9. android 二 BMI 小例子
  10. Android 3.0 r1 API中文文档(107) —— A