延续百度地图定位的Demo。採用Service来进行百度定位,并且将数据上传到server上遇到了一个问题:在真机中使用清理内存来关闭程序的之后,Service会被关闭,可是过几秒中,它又会自己主动重新启动。重新启动就算了,并且再次登陆系统的时候,又会开启一个一样的服务,在LogCat中就会看到每次都获取到两次的定位数据。

然后想想能否够在建立Service之前推断这个服务有没有被创建?仅仅要能做这个推断。那么服务存在我们就无论它,假设不存在则创建。本着这个思路,百度发现可行(Service后台服务创建时最好都要推断是否存在),代码例如以下:

private boolean isWorked(String className) {
ActivityManager myManager = (ActivityManager) LoginActivity.this
.getApplicationContext().getSystemService(
Context.ACTIVITY_SERVICE);
ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager
.getRunningServices(30);
for (int i = 0; i < runningService.size(); i++) {
if (runningService.get(i).service.getClassName().toString()
.equals(className)) {
return true;
}
}
return false;
}

推断过程:

if(!this.isWorked("包.服务名")){
Intent intent = new Intent();
intent.setAction("该服务组件的intent-filter的action");
// 启动Service
startService(intent);
}
else{
Log.i("info", "服务已经启动了! ! ");
}

Android——推断Service是否已经启动的更多相关文章

  1. Android中Service通信(一)——启动Service并传递数据

    启动Service并传递数据的小实例(通过外界与服务进行通信): 1.activity_main.xml: <EditText android:layout_width="match_ ...

  2. Android service介绍和启动方式

    1.Android service的作用: service通常是用来处理一些耗时操作,或后台执行不提供用户交互界面的操作,例如:下载.播放音乐. 2.Android service的生命周期: ser ...

  3. android Service简介及启动关闭方式

    (1)Service是Android系统中的四大组件之一,和Activity是同一层次的组件:它是一种生命周期较长,没有可视化界面,运行于后台的一种服务:例如,我们听音乐可以使用Service,下载东 ...

  4. android中service启动后台程序

    Service是Android中一个类,它是Android四大组件之一,使用Service可以在后台执行长时间的操作( perform long-running operations in the b ...

  5. Android服务(Service)研究

    Service是android四大组件之一,没有用户界面,一直在后台运行. 为什么使用Service启动新线程执行耗时任务,而不直接在Activity中启动一个子线程处理? 1.Activity会被用 ...

  6. Android中Service和Activity之间的通信

    启动Service并传递数据进去: Android中通过Intent来启动服务会传递一个Intent过去. 可以在Intent中通过putExtra()携带数据 Intent startIntent ...

  7. Android中Service 使用详解(LocalService + RemoteService)

    Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

  8. Android中Service(服务)详解

    http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...

  9. 【Android】应用程序Activity启动过程分析

    在Android系统中,有两种操作会引发Activity的启动,一种用户点击应用程序图标时,Launcher会为我们启动应用程序的主Activity:应用程序的默认Activity启动起来后,它又可以 ...

随机推荐

  1. sql小计合计

    转自:http://www.jb51.net/article/18860.htm 这里介绍sql server2005里面的一个使用实例: CREATE TABLE tb(province nvarc ...

  2. Gym - 101981A The 2018 ICPC Asia Nanjing Regional Contest A.Adrien and Austin 简单博弈

    题面 题意:一堆有n个石子,编号从1⋯N排成一列,两个人Adrien 和Austin玩游戏,每次可以取1⋯K个连续编号的石子,Adrien先手,谁不能取了则输 题解:k==1时,显然和n奇偶相关,当k ...

  3. HDU3085 Nightmare Ⅱ

    题目: Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were ...

  4. tp 推送微信的模板消息

    设置推送类: <?php /** * tpshop 微信支付插件 * ============================================================== ...

  5. E - A Trivial Problem(求满足x!的尾数恰好有m个0的所有x)

    Problem description Mr. Santa asks all the great programmers of the world to solve a trivial problem ...

  6. Halcon学习笔记之支持向量机(二)

    例程:classify_halogen_bulbs.hdev 在Halcon中模式匹配最成熟最常用的方式该署支持向量机了,在本例程中展示了使用支持向量机对卤素灯的质量检测方法.通过这个案例,相信大家可 ...

  7. Android RxVolley = Volley + RxJava + OkHttp

    Github:https://github.com/kymjs/RxVolley RxVolley使用文档 V1.0:http://rxvolley.mydoc.io/ 一.RxVolley使用指南 ...

  8. 【技术累积】【点】【java】【18】URLEncode

    基础概念 由于以URL的形式传递信息给服务器时,不允许URL中出现一些特殊字符和空格的,所以需要对URL进行编码处理. 原理是: 将要转码的字符转变为16进制: 从右到左,每两位前面加% 哪些字符是需 ...

  9. 偏函数应用(Partial Application)和函数柯里化(Currying)

    偏函数应用指的是固化函数的一个或一些参数,从而产生一个新的函数.比如我们有一个记录日志的函数: 1: def log(level, message): 2: print level + ": ...

  10. BZOJ2251 [2010Beijing Wc]外星联络 后缀数组 + Height数组

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in", "r", stdin ...