Search code examples
androidservicebroadcastreceiverandroid-broadcast

Android how to self start my application when my device powered on?


I am working on android application,i just want to when i powered off my device,when i start again my device my application should self start,is it possible to the android?if yes,please help,any help will be highly appreciated.Thanks.

my custom broadcast reciever class:

package com.whizpool.nogademo;

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

public class PowerEventReceiver extends BroadcastReceiver {

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

    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        Intent i = new Intent(context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }

}
}

my menifast file:

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

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.whizpool.nogademo.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape"
        android:windowSoftInputMode="adjustNothing" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <service android:name="com.whizpool.nogademo.AEScreenOnOffService" >
        <intent-filter>
            <action android:name="com.whizpool.nogademo.AEScreenOnOffService" />
        </intent-filter>
    </service>
    <receiver android:name="com.whizpool.nogademo.PowerEventReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.ACTION_SHUTDOWN" />
        </intent-filter>
    </receiver>

    <!--  <receiver
        android:name=".AutoRestart"
        android:enabled="true"
        android:exported="true" >
        <intent-filter >
            <action android:name="android.intent.action.PACKAGE_INSTALL" />
            <action android:name="android.intent.action.PACKAGE_CHANGED" />
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <action android:name="android.intent.action.PACKAGE_RESTARTED" />

        </intent-filter>
    </receiver> -->

    <!-- <receiver android:name="com.whizpool.nogademo.MyBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver> -->
 </application>


</manifest>

Solution

  • Added a reciver which handles the power events and start your activity in this

    public class PowerEventReceiver extends BroadcastReceiver {
    
            @Override
            public void onReceive(Context context, Intent intent) {
    
                if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
                    Intent i = new Intent(context, HeliosActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);
                }
    
            }
        }
    

    and add this to your manifest

      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
           <receiver android:name="com.RareMediaCompany.Helios.PowerEventReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.ACTION_SHUTDOWN" />
                </intent-filter>
            </receiver>