1

I wanted to execute a piece of code as soon as every time Device is booted up.

Taking help from the answer provided here.

I have created an application which only contains one class which extends BroadcastReceiver. (There is no other activity/service as on now)

Currently ony for testing purpose, my BroadcastReceiver should create a file 'Log.txt' on SD card (if not exits) and append the file with current time stamp.

But right now I can not see any file getting created.

Please suggest where am I wrong.

Code:

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;
    }

}

Manifest file:

<?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>
Community
  • 1
  • 1
reiley
  • 3,759
  • 12
  • 58
  • 114
  • If you are not getting an exception thrown, you could add a debug Log statement just inside the block that checks for external storage being readable/writable. What do you see? – Michael Krause Oct 22 '14 at 20:03
  • do you see "Done" in your logcat? – pskink Oct 22 '14 at 20:04
  • @pskink - no nothing is printed – reiley Oct 22 '14 at 20:05
  • What is the package for your ServiceStarter class? It should match what you've got in your manifest file. (com.android.test). By the way, you should consider using a different package name that doesn't conflict with Android itself. – Michael Krause Oct 22 '14 at 20:06
  • have you run at least one Activity of your app? – pskink Oct 22 '14 at 20:11
  • @pskink I've no activity in my app. I wanted to create application with only service that triggers on boot. But right now I'm not able to trigger this broadcast receiver on boot. – reiley Oct 22 '14 at 20:20
  • see http://blog.vogella.com/2011/12/11/automatically-starting-services-in-android-after-booting/ – pskink Oct 22 '14 at 20:38

0 Answers0