0

I have an application that starts a thread in background. This thread read the logcat:

public class MonitorLogService extends Service{

...

    private void handleCommand(Intent intent){
    if (ACTION_FOREGROUND.equals(intent.getAction())) {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.app_name);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.stat_notify, text, System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ConfigActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, text,getText(R.string.service_running), contentIntent);
        startForegroundCompat(R.string.service_running, notification);

        monitorLogThread = new MonitorLogThread();
        monitorLogThread.start();
    }
}

private static Thread monitorLogThread;

private class MonitorLogThread extends Thread{

    BufferedReader br; 

    @Override
    public void run() {
        try {   

                Process process;
                process = Runtime.getRuntime().exec("logcat -c");
                process = Runtime.getRuntime().exec("logcat -b events");
                br = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;

                // Lee mientras no haya líneas nulas o el proceso se interrumpa
                while(((line=br.readLine()) != null) && !this.isInterrupted()){
                    Log.d("MyApp",line);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}

Everything works fine in Android 2.3 but I have a problem in Android 4: "br.readLine()" doesn't return any line when I leave the app, it's just paused. When I open again the application, the service return the logcat lines.

I don't understand what happend because in Android 2.3 everything works fine.

Thanks

esteban
  • 543
  • 4
  • 18

2 Answers2

0

The command logcat -b events dump the buffer, but stills attached to the buffer waiting additional events and will never exit.

To have logcat exiting after reading the information in the buffer, you need to add the flag -d.

Example:

logcat -d -b events

Regards.

Luis
  • 11,978
  • 3
  • 27
  • 35
  • Thanks for your answer Luis but attach the buffer waiting for additional events and never exit is precisely what I need. – esteban Nov 16 '12 at 08:29
  • Sorry, I misunderstood it. I've tried you code in a test app (SDK 16), added uses permission `READ_LOGS`, and service in `START_STICKY` mode and it runs fine. Keeps logging events, even after exiting activity. The problem should be somewhere else. – Luis Nov 16 '12 at 11:14
  • I use permission READ_LOGS and service START_STICKY as it's done in the samples demo file "ForegroundService.java" from the sdk documentation. I edited my question and you can see how i call the thread. Thanks. – esteban Nov 16 '12 at 15:51
  • I suspect that the problem is the API 16 doesn't support the logs reading permission as I found here http://stackoverflow.com/questions/11461650/read-logs-permission-on-jelly-bean-api-16 – esteban Nov 19 '12 at 09:26
  • You are correct. I've done the test above in emulator, and it seems only to affect real devices. – Luis Nov 19 '12 at 13:26
0

Unfortunately the API 16 doesn't support the logs reading permission as I found here.

Community
  • 1
  • 1
esteban
  • 543
  • 4
  • 18