请教一个anr问题
我写了个服务,功能是每十秒钟检查某个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'
但是在检查第二次之后就会出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'
...全文
【问题分析】WMS无焦点窗口的ANR问题【Android 14】 用户操作出的 Launcher ANR,场景为在Launcher界面一个Activity启动又快速销毁导致的无焦点窗口问题。 阅读详情
相关推荐
ANR的监测与定位Demo
这种情况严重影响用户体验,因此开发者需要对ANR进行及时监控和定位。本"ANR的监测与定位Demo"提供了这样一个工具,它能帮助开发者实时监测应用中的ANR问题,并在出现ANR时迅速定位到问题根源。
Android-ANR-WatchDog一个用于检测AndroidANR(应用不响应)的watchdog
`Android-ANR-WatchDog`是一个专门为解决这个问题而设计的开源工具,它能够帮助开发者监控并处理ANR错误,从而提高应用的稳定性和用户体验。
ANR问题分析与解决[源码]
在Android应用开发中,ANR问题是一个不容忽视的问题。只有通过深入分析和理解ANR的成因,才能有效地预防和解决这一问题,提升应用的性能和用户体验。
ANR各种日志.rar
如果超时,系统会记录一个ANR错误。开发者应检查广播接收器的代码,确保其尽可能地快速执行,并避免在主线程中进行耗时操作。
android7 关闭ANR对话框代码
在Android系统中,ANR(Application Not Responding)是一个常见的问题,它表示应用程序无响应,通常出现在用户界面长时间无法交互时。



