0

我有一个在后台启动线程的应用程序。该线程读取了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();
            }
    }
}

在 Android 2.3 中一切正常,但在 Android 4 中有一个问题:当我离开应用程序时, “br.readLine()”没有返回任何行,它只是暂停了。当我再次打开应用程序时,服务返回 logcat 行。

我不明白发生了什么,因为在 Android 2.3 中一切正常。

谢谢

4

2 回答 2

0

该命令logcat -b events转储缓冲区,但仍附加到缓冲区等待其他事件并且永远不会退出。

logcat在读取缓冲区中的信息后退出,您需要添加标志-d

例子:

logcat -d -b events

问候。

于 2012-11-13T16:19:07.850 回答
0

不幸的是,API 16 不支持我在这里找到的日志读取权限。

于 2012-11-19T16:02:29.950 回答