Thursday, July 12, 2012

How to start an activity just after booting of a mobile device

This post is an example of how an application will work just after the booting of the device.Here the example shows an authentication app which will work as a lock to the device.

1) Define in the AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.boot"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
  <receiver android:enabled="true" android:name=".BootActivity"
        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>
  <activity android:name=".AutoStartActivity">
    </activity>
    </application>
    </manifest>


2) Create BootActivity.java in src with the following code
Here AutoStartActivity is the activity we have to run after booting


package com.boot;

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

public class BootActivity extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, AutoStartActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
}
}

3)The XML (main.xml) for the activity is


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:gravity="center">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" android:gravity="center_horizontal">

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/button1"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Login" />

    </LinearLayout>

</LinearLayout>




4) Create the activity AutoStartActivity

package com.boot;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AutoStartActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText ed=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(ed.getText().toString().equals("abc123"))
{
AutoStartActivity.this.finish();
}
else
{
Toast.makeText(AutoStartActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
}
});
}

@Override
public void onAttachedToWindow() {
   this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
   super.onAttachedToWindow();
}
}


  • Run it in a new avd


1 comment:

  1. hi, i tried it but its not working.it shows no error but it doesnot open the application after booting.i am using android 4.2.2 Nexus 7. help please.

    ReplyDelete

MVC - MVP : Difference between these design patterns?

In traditional UI development - developer used to create a  View  using window or usercontrol or page and then write all logical code ...