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 に答える