How to Run an andriod appltion when device Start up?
<p>I need to run my application when the device is boot up. I tried the following code. Here my problem is, when i run my application once and i <strong>restart</strong> my device means it works, if i <strong>switch off my device and then switched on</strong> means auto run won't works. Why it will happens?</p>

<p><strong>MainActivity.java</strong></p>

<pre><code>public class MainActivity extends Activity {

    ImageView mImageView;
    Button swap_btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView) findViewById(R.id.imageView1);
        swap_btn = (Button) findViewById(R.id.button1);
        mImageView.setImageResource(R.drawable.image_01);
        swap_btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                mImageView.setImageResource(R.drawable.image_02);

            }
        });
    }

}
</code></pre>

<p><strong>OpenOurAppReceiver.java</strong></p>

<pre><code>public class OpenOurAppReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }

}
</code></pre>

<p><strong>Manifest.xml</strong></p>

<pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bootup_sample_app"
    android:versionCode="1"
    android:versionName="1.0" &gt;

    &lt;uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" /&gt;

    &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;

    &lt;application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" &gt;
        &lt;activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

        &lt;receiver
            android:name=".OpenOurAppReceiver"
            android:enabled="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;

                &lt;category android:name="android.intent.category.DEFAULT" /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;
    &lt;/application&gt;

&lt;/manifest&gt;
</code></pre>
