Tuesday 9 April 2013

Run Application on Device Start


In this post, I will explain how to start our application on start up i.e. on BOOT_COMPLETED.
Let's take a look
Create a new application with the following details:



Now go to Package Explorer, create a Service:
To create a Service, right click on our project:
RunAppOnBoot.
Select New --> Other... --> From Wizard Select Android Object
Select Service --> Give Class Name --> Check Enabled
Click Next --> Finish


You will get a new file, MyService.java
Normally Services are used to perform the background task. It keeps running in the device even the activity is destroyed.


We need a BroadcastReceiver to receive the BOOT_COMPLETED event.

Perform the steps as mentioned above. Instead of Service, select Broadcast Receiver. Check Enabled and Exported.
You will get another new file i.e. MyReceiver.java


Now Go to AndroidManifest.xml

Do the following changes:

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <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.samples.runapponboot.RunOnBootActivity"
            android:label="@string/app_name" >
        </activity>

        <service
            android:name="com.samples.runapponboot.MyService"
            android:enabled="true" >
        </service>


        <receiver
            android:name="com.samples.runapponboot.MyReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>


MyService.java

package com.samples.runapponboot;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
    // Broadcast Receiver
    private MyReceiver mMyReceiver;
    // Intent Filter to Receive BOOT_COMPLETED
    private IntentFilter mIntentFilter;
   
    public MyService() {
    }

    @Override
    public void onCreate() {
        // Register MyReciever to Receive BOOT_COMPLETED
        try {
            mMyReceiver = new MyReceiver();
            mIntentFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
            registerReceiver(mMyReceiver, mIntentFilter);
        } catch (Exception e) {
            Log.e("", e.getMessage());
        }
    }
   
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mMyReceiver != null) {
            unregisterReceiver(mMyReceiver);
        }
    }
   
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}



MyReceiver.java
 
package com.samples.runapponboot;

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

public class MyReceiver extends BroadcastReceiver {
    // Intent for RunOnBootActivity
    private Intent mRunOnBootIn;
   
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            mRunOnBootIn = new Intent(context, RunOnBootActivity.class);
            mRunOnBootIn.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mRunOnBootIn);
        } catch (Exception e) {
            Log.e("MyReceiver: inReceive()", e.getMessage());
        }
    }
}


activity_run_on_boot.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".RunOnBootActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>


It will make the application run at the device start.

No comments:

Post a Comment