请教一个anr问题

ActionWind 初级 2018-03-18 09:18:26
我写了个服务,功能是每十秒钟检查某个app有没有运行,如果没有则启动它。
但是在检查第二次之后就会出anr问题:D/ANRAppManager(2229): !!! It is not under singleton mode, U can't use it. !!!
请问问什么会出现这个问题,应该怎么解决?
源代码:
package run.lock;
import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class ALAService extends Service {

@Override
public IBinder onBind(Intent arg0) {
// state = BIND;
return null;
}

@Override
public void onCreate() {
super.onCreate();
System.out.println("========================Service onCreate--->");

Boolean isRun=false;
String appName="run.wordsstudy";//所要检查的app包名
int i=0;

// 每隔10秒检查一次应用锁是否运行,没运行则启动它
while (true) {
i++;
System.out.println("======================== "+i+" --->");
// 获取activity管理对象
ActivityManager activityManager = (ActivityManager) this
.getSystemService(Context.ACTIVITY_SERVICE);
// 获取所有正在运行的app
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();

// 遍历app,获取应用名称或者包名
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.contains(appName)) {
isRun = true;
break;
}
}
if(isRun==false){
//如果app没有运行,则启动它
Intent intent = getPackageManager().getLaunchIntentForPackage(appName);
if (intent != null) {
//intent.putExtra("type", "110");//有空查一下这函数什么意思。好像没什么用
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}

// 等待10秒
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("========================Service onStartCommand--->");
return START_STICKY;// 此关键字会在此app被关闭时让系统将此服务重启
}

}


日志文件:
03-18 21:03:09.547: D/jdwp(2229): received file descriptor 49 from ADB
03-18 21:03:11.548: D/dalvikvm(2229): Elevating priority from 0 to 10
03-18 21:03:58.859: V/ActivityThread(2229): ActivityRecord{4252dd20 token=android.os.BinderProxy@4252d5f8 {run.lock/run.lock.MainActivity}}: app=android.app.Application@4252c1c8, appName=run.lock, pkg=run.lock, comp={run.lock/run.lock.MainActivity}, dir=/data/app/run.lock-1.apk
03-18 21:03:58.865: I/System.out(2229): ========================app onCreate--->
03-18 21:03:58.873: V/ActivityThread(2229): Performing resume of ActivityRecord{4252dd20 token=android.os.BinderProxy@4252d5f8 {run.lock/run.lock.MainActivity}} finished=false
03-18 21:03:58.873: D/ActivityThread(2229): ACT-AM_ON_RESUME_CALLED ActivityRecord{4252dd20 token=android.os.BinderProxy@4252d5f8 {run.lock/run.lock.MainActivity}}
03-18 21:03:58.873: V/ActivityThread(2229): Resume ActivityRecord{4252dd20 token=android.os.BinderProxy@4252d5f8 {run.lock/run.lock.MainActivity}} started activity: false, hideForNow: false, finished: false
03-18 21:03:58.903: V/PhoneWindow(2229): DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.impl.PhoneWindow$DecorView{4253b5f8 I.E..... R.....ID 0,0-0,0}
03-18 21:03:58.907: V/ViewRootImpl(2229): mApplyParallel = false, this = ViewRoot{425626c8 ,ident = 0}
03-18 21:03:58.911: V/ActivityThread(2229): Resuming ActivityRecord{4252dd20 token=android.os.BinderProxy@4252d5f8 {run.lock/run.lock.MainActivity}} with isForward=true
03-18 21:03:58.911: V/PhoneWindow(2229): DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{425626c8 run.lock/run.lock.MainActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{4253b5f8 V.E..... R.....ID 0,0-0,0}
03-18 21:03:58.911: V/ActivityThread(2229): Scheduling idle handler for ActivityRecord{4252dd20 token=android.os.BinderProxy@4252d5f8 {run.lock/run.lock.MainActivity}}
03-18 21:03:58.912: D/ActivityThread(2229): ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{4252dd20 token=android.os.BinderProxy@4252d5f8 {run.lock/run.lock.MainActivity}}
03-18 21:03:58.913: D/ActivityThread(2229): SVC-Creating service: CreateServiceData{token=android.os.BinderProxy@42537578 className=run.lock.ALAService packageName=run.lock intent=null}
03-18 21:03:58.913: I/System.out(2229): ========================Service onCreate--->
03-18 21:03:58.913: I/System.out(2229): ======================== 1 --->
03-18 21:04:08.935: I/System.out(2229): ======================== 2 --->
03-18 21:04:18.920: D/ANRAppManager(2229): !!! It is not under singleton mode, U can't use it. !!!
03-18 21:04:18.937: I/System.out(2229): ======================== 3 --->
03-18 21:04:18.979: I/dalvikvm(2229): threadid=3: reacting to signal 3
03-18 21:04:18.980: D/dalvikvm(2229): dump native stack for : Binder_2
03-18 21:04:18.980: D/dalvikvm(2229): unwind backtrace
03-18 21:04:18.980: D/Corkscrew(2229): Unwinding thread 2242 from thread 2234.
03-18 21:04:18.991: D/dalvikvm(2229): get native symbol
03-18 21:04:18.991: D/dalvikvm(2229): format backtrace line
03-18 21:04:18.991: D/dalvikvm(2229): get native done
03-18 21:04:18.991: D/dalvikvm(2229): dump native stack for : Binder_1
03-18 21:04:18.991: D/dalvikvm(2229): unwind backtrace
03-18 21:04:18.991: D/Corkscrew(2229): Unwinding thread 2241 from thread 2234.
03-18 21:04:18.994: D/dalvikvm(2229): get native symbol
03-18 21:04:18.994: D/dalvikvm(2229): format backtrace line
03-18 21:04:18.994: D/dalvikvm(2229): get native done
03-18 21:04:18.995: D/dalvikvm(2229): dump native stack for : Compiler
03-18 21:04:18.995: D/dalvikvm(2229): unwind backtrace
03-18 21:04:18.995: D/Corkscrew(2229): Unwinding thread 2236 from thread 2234.
03-18 21:04:19.001: D/dalvikvm(2229): get native symbol
03-18 21:04:19.001: D/dalvikvm(2229): format backtrace line
03-18 21:04:19.001: D/dalvikvm(2229): get native done
03-18 21:04:19.004: D/dalvikvm(2229): dump native stack for : JDWP
03-18 21:04:19.004: D/dalvikvm(2229): unwind backtrace
03-18 21:04:19.004: D/Corkscrew(2229): Unwinding thread 2235 from thread 2234.
03-18 21:04:19.012: D/dalvikvm(2229): get native symbol
03-18 21:04:19.012: D/dalvikvm(2229): format backtrace line
03-18 21:04:19.012: D/dalvikvm(2229): get native done
03-18 21:04:19.012: D/dalvikvm(2229): dump native stack for : GC
03-18 21:04:19.012: D/dalvikvm(2229): unwind backtrace
03-18 21:04:19.012: D/Corkscrew(2229): Unwinding thread 2233 from thread 2234.
03-18 21:04:19.014: D/dalvikvm(2229): get native symbol
03-18 21:04:19.014: D/dalvikvm(2229): format backtrace line
03-18 21:04:19.014: D/dalvikvm(2229): get native done
03-18 21:04:19.014: D/dalvikvm(2229): Prepare stack traces for '/data/anr/traces.txt' done
03-18 21:04:19.014: I/dalvikvm(2229): Wrote stack traces to '/data/anr/traces.txt'
...全文
972 1 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ActionWind 初级 2018-03-28
谢谢楼上的的两位,就是那样的原因造成的。结贴给分。
回复
cattpon 2018-03-20
http://blog.csdn.net/m0_37754543/article/details/70237772
回复
键盘舞者113 2018-03-19
他的意思是不能开启多个同类Service
回复
江三木洛 2018-03-19
这个anr问题不是因为第二次检查,是因为在service中做了耗时操作,你开个线程,然后把检查app的while逻辑扔进去
回复
x443137254 2018-03-19
好长没看,只看见第一句,让你用singleton mode,去试试呗
回复
相关推荐
发帖
Android
创建于2009-10-09

7.8w+

社区成员

移动平台 Android
申请成为版主
帖子事件
创建了帖子
2018-03-18 09:18
社区公告
暂无公告