0

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 restart my device means it works, if i switch off my device and then switched on means auto run won't works. Why it will happens?

MainActivity.java

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

            }
        });
    }

}

OpenOurAppReceiver.java

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

}

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bootup_sample_app"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".OpenOurAppReceiver"
            android:enabled="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />

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

</manifest>
MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
Aerrow
  • 12,086
  • 10
  • 56
  • 90
  • this has come up too many times. If i understood you correctly then since ICS the app has to be opened atleast once before broadcastreciever starts to work – nandeesh Sep 06 '12 at 11:49
  • you have to run the app at least 1 s fr the app to receive boot complete event – Athul Harikumar Sep 06 '12 at 11:51
  • @nandeesh: Thanks for your quick response, is there any other way to start my application when device will turns. If may the app won't run previously? – Aerrow Sep 06 '12 at 11:51
  • the only way is i hope to recive the screen locked unlocked event but i am not sure – Athul Harikumar Sep 06 '12 at 11:53
  • @droidhot: Thanks for your response, if you have this concept (to use screen lock/unlock) means, kindly guide me. – Aerrow Sep 06 '12 at 11:57
  • i am realy sorry i have read 1s i think from commons ware in SO itself but i dont remember fully just search through this site its there – Athul Harikumar Sep 06 '12 at 11:59
  • not possible from 3.1 onwards reffer http://stackoverflow.com/questions/8531926/how-to-start-a-service-when-apk-is-installed-for-the-first-time this – Athul Harikumar Sep 06 '12 at 12:17

1 Answers1

0

If you have a user interface that should be visible when the device is powered on, make it be a home screen.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • As you say in this question http://stackoverflow.com/questions/8531926/how-to-start-a-service-when-apk-is-installed-for-the-first-time we cant run our application when device boot up in android version 3.1+, is there any alternative is possible to do this. – Aerrow Sep 06 '12 at 14:22
  • @Aerrow: Fortunately, no. I repeat: if you have a user interface that should be visible when the device is powered on, make it be a home screen. – CommonsWare Sep 06 '12 at 14:24