Android logcat information is recorded to the phone file

Add permissions:

    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Sample code:

public class MyApp extends Application {

    private Process mLogProcess;

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            int pid = android.os.Process.myPid();
            Log.d("MyApp", "PID is " + pid);
            Calendar calendar = Calendar.getInstance();
            String filename = String.format("/sdcard/MyApp_Log_%04d%02d%02d_%02d%02d%02d.txt",
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH),
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    calendar.get(Calendar.SECOND));
            mLogProcess = Runtime.getRuntime().exec(String.format("logcat -v time -f %s", filename));
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

    @Override
    public void onTerminate() {
        super.onTerminate();

        if(mLogProcess != null ){
            mLogProcess.destroy();
        }
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325761614&siteId=291194637