I have a service which has been working perfectly for weeks, and in the last day has suddenly started having problems. I know I've added something recently which triggered this behavior but looking back through my GIT logs I can't find anything that seems obvious so I'm going to try to see if anyone can help me understand what is actually happening instead.
From a button click in a fragment I call the following method:
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.");
}
I see the "Intent was broadcast" message in my log, so I know I've made it that far.
The service's onCreate()
method is as follows:
@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>();
}
The service's onStartCommand()
method is as follows:
@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;
}
None of the log statements from onStartCommand()
or onCreate()
for the service are ever called. Instead, I get the following stack trace and an 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'
I have added strict mode logging in my application's 'onCreate()' method, but it does not report anything:
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
And of course I have my service declared in my manifest (it has been working until today).
So ultimately my question is, what would cause an intent to send successfully but have an ANR reported rather than starting the service? Why is StrictMode not detecting it?
REQUESTED ADDITIONAL INFORMATION
The intent is passed to the performEnterCalibrationMode(Intent)
method by the following code:
final Intent intent = new Intent(mContext, CommunicationService.class);
intent.setAction(ServiceAction.EXECUTE_TASK.toString());
UPDATE
It turns out the service is being started prior to this (I am able to send commands to it to connect). However, when I send this command after connecting (a second or two), thats when I get the ANR.