Android ANR在尝试启动服务时

时间:2014-09-06 23:06:40

标签: android service android-anr-dialog

我有一项服务已经完美地工作了几个星期,并且在最后一天突然开始出现问题。我知道我最近添加了一些引发这种行为的东西,但回头看看我的GIT日志,我找不到任何看似显而易见的东西,所以我试着看看是否有人可以帮我理解什么实际上正在发生。

从按钮中单击片段,我调用以下方法:

private void performEnterCalibrationMode(Intent intent) {
        Log.d(TAG, "Creating calibration mode task.");
        final CalibrationModeTask calTask = new CalibrationModeTask(CALIBRATION_MODE.ENTER_CALIBRATION_MODE);
        intent.putExtra(TekCast.EXTRA_TASK, calTask);
        intent.putExtra(TekCast.EXTRA_TASK_COMPLETE_CALLBACK, this);
        Log.d(TAG, "Broadcasting calibration mode task intent.");
        mContext.startService(intent);
        Log.d(TAG, "Intent was broadcast.");
}

我看到"意图被广播"在我的日志中留言,所以我知道我已经做到了这么远。

服务的onCreate()方法如下:

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "CommunicationService onCreate()");

    // Setup the background thread and its controls
    HandlerThread thread = new HandlerThread("TekDAQC Communication Service", Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();

    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper, this);
    mLocalBroadcastMgr = LocalBroadcastManager.getInstance(getApplicationContext());

    // Initialize the session map
    mCommSessions = new ConcurrentHashMap<String, ASCIICommunicationSession>();
}

服务的onStartCommand()方法如下:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // Extract the service instruction
    final String action = intent.getAction();
    Log.d(TAG, "Received start command: " + action);
    // Build the message parameters
    Bundle extras = intent.getExtras();
    if (extras == null)
        extras = new Bundle();
    extras.putString(TekCast.EXTRA_SERVICE_ACTION, action);

    // Run each task in a background thread.
    final Message msg = mServiceHandler.obtainMessage();
    msg.arg1 = startId;
    msg.setData(extras);
    Log.d(TAG, "Sending message to background thread.");
    mServiceHandler.sendMessage(msg);
    return Service.START_NOT_STICKY;
}

从未调用onStartCommand()onCreate()的服务日志语句。相反,我得到以下堆栈跟踪和ANR:

09-06 19:00:58.291: D/CalibrationSteps(2228): Creating calibration mode task.
09-06 19:00:58.291: D/CalibrationSteps(2228): Broadcasting calibration mode task intent.
09-06 19:00:58.601: I/art(2228): GcCauseBackground sticky partial concurrent mark sweep GC freed 50607(2MB) AllocSpace objects, 5(202KB) LOS objects, 5% free, 37MB/39MB, paused 9.869ms total 72.321ms
09-06 19:00:59.361: I/art(2228): GcCauseBackground sticky partial concurrent mark sweep GC freed 17890(841KB) AllocSpace objects, 0(0B) LOS objects, 5% free, 39MB/41MB, paused 11.853ms total 19.861ms
09-06 19:00:59.371: D/CalibrationSteps(2228): Intent was broadcast.
09-06 19:00:59.581: I/System.out(2228): Discovery timed out.
09-06 19:00:59.581: I/System.out(2228): Closing socket...
09-06 19:01:19.431: I/art(2228): Thread[5,tid=2234,WaitingInMainSignalCatcherLoop,Thread*=0x4749b3e8,peer=0x65551c10,"Signal Catcher"]: reacting to signal 3
09-06 19:01:19.511: I/art(2228): Wrote stack traces to '/data/anr/traces.txt'

我在我的应用程序中添加了严格模式记录&#39; onCreate()&#39;方法,但它没有报告任何内容:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());

当然,我在我的清单中声明了我的服务(它一直工作到今天)。

所以最终我的问题是,什么会导致意图成功发送但是报告了ANR而不是启动服务?为什么StrictMode没有检测到它?

请求补充资料

通过以下代码将意图传递给performEnterCalibrationMode(Intent)方法:

final Intent intent = new Intent(mContext, CommunicationService.class);
intent.setAction(ServiceAction.EXECUTE_TASK.toString());

更新

事实证明服务正在此之前启动(我能够向其发送命令以进行连接)。但是,当我在连接(一两秒)之后发送此命令时,那就是当我得到ANR时。

0 个答案:

没有答案