开机启动时将log日志写入到SDcard

需求分析:

手机开机启动后,开始收集log日志信息并且将日志保存到SDcard。

功能实现:

(1)Manifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sony.logdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="16" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name=".LogCatcherReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

        <service android:name="CatchLogService" >
            <intent-filter>
                <action android:name="com.sony.intent.action.CATCH_LOG" />
            </intent-filter>
        </service>
    </application>

</manifest>


(2)开机启动后收到系统广播"android.intent.action.BOOT_COMPLETED",然后启动后台service,将log写入SDcard。

LogCatcherReceiver接收器:

package com.sony.logdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class LogCatcherReceiver extends BroadcastReceiver {
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.i("LogCatcherReceiver","receive log");
        context.startService(new Intent(context,
                CatchLogService.class));
    }
}

后台Service:

package com.sony.logdemo;

import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class CatchLogService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LogCatcherReceiver", "start service");
        catchLog();
        // TODO Auto-generated method stub
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    public void catchLog() {
        new Thread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                String shell = "logcat";
                try {
                    Process process = Runtime.getRuntime().exec(shell);
                    InputStream inputStream = process.getInputStream();
                    boolean sdCardExist = Environment.getExternalStorageState().equals(
                            android.os.Environment.MEDIA_MOUNTED);
                    File dir = null;
                    if (sdCardExist) {
                        dir = new File(Environment.getExternalStorageDirectory().toString()
                                + File.separator + "logcat.txt");
                        Log.i("LogCatcherReceiver", Environment.getExternalStorageDirectory()
                                .toString() + File.separator + "logcat.txt");
                        if (!dir.exists()) {
                            dir.createNewFile();
                        }

                    }
                    byte[] buffer = new byte[1024];
                    try {
                        if (dir == null) {
                            Log.i("LogCatcherReceiver", "file==null");
                        }
                        FileOutputStream fos = new FileOutputStream(dir);
                        int read = 0;
                        try {
                            {
                                while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
                                    fos.write(buffer, 0, read);
                                }
                            }
                        } finally {
                            fos.close();
                            }
                    } finally {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

}


service中让线程休眠了5秒,否则会crash,刚启动时SDcard为加载,sdCardExist=false.

猜你喜欢

转载自blog.csdn.net/oWeiXiao123/article/details/9297675