BOOT_COMPLETED上的BroadcastReceiver没有被调用

时间:2014-10-22 19:51:19

标签: android android-broadcast

我希望每次启动设备时都会执行一段代码。

从提供的答案here获得帮助。

我创建了一个只包含一个扩展BroadcastReceiver的类的应用程序。 (现在没有其他activity/service

目前为了测试目的,我的BroadcastReceiver应该创建一个文件' Log.txt '在SD卡上(如果没有退出)并附加当前时间戳的文件。

但是现在我看不到任何文件被创建了。

请说明我错在哪里。

代码:

public class ServiceStarter extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (isExternalStorageWritable() && isExternalStorageReadable()) {
            String root = Environment.getExternalStorageDirectory().toString();
            File myDir = new File(root + "/saved_text"); // After boot this saved_text is not getting created
            if (!myDir.exists()) {
                myDir.mkdirs();
            }
            String fname = "Log.txt";
            File file = new File(myDir, fname);

            try {
                PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
                out.println("logged :: " + timeStamp);
                out.close();

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

        System.out.println("Done");

    }

    /* Checks if external storage is available to write */
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    /* Checks if external storage is available to at least read */
    public boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }

}

清单文件:

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

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="21" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- Start the Service if applicable on boot -->
        <receiver
            android:name="com.android.test.ServiceStarter"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

0 个答案:

没有答案