In Previous post I have listed some broadcast action.In this post I will use one of broadcast action.
Sometimes we require to launch application whenever user restart device.We can achieve this using broadcast receiver.Through following application I have expalin how to launch application on device reboot.
Auto Start Application :
Required Steps :
1.Create SampleActivity which will be launcher activity of application.:
package com.androindu;
import android.app.Activity;
import android.os.Bundle;
public class SampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
2.Create Broadcast Receiver.which will receive reboot action of device:
I have override onReceive() method, where I have started launcher activity of application.
package com.androindu;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class LaunchAppBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(context, SampleActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
}
3.Edit Manifest file and register Broadcast receiver for "android.intent.action.BOOT_COMPLETED" action.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androindu"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SampleActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="LaunchAppBroadcast"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
Layout file main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Launch on reboot of device"
/>
</LinearLayout>
Thus
Summary :
With this post we can get idea about how to use Broadcast receiver.
Sometimes we require to launch application whenever user restart device.We can achieve this using broadcast receiver.Through following application I have expalin how to launch application on device reboot.
Auto Start Application :
Required Steps :
1.Create SampleActivity which will be launcher activity of application.:
package com.androindu;
import android.app.Activity;
import android.os.Bundle;
public class SampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
2.Create Broadcast Receiver.which will receive reboot action of device:
I have override onReceive() method, where I have started launcher activity of application.
package com.androindu;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class LaunchAppBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(context, SampleActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
}
3.Edit Manifest file and register Broadcast receiver for "android.intent.action.BOOT_COMPLETED" action.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androindu"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SampleActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="LaunchAppBroadcast"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
Layout file main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Launch on reboot of device"
/>
</LinearLayout>
Thus
Summary :
With this post we can get idea about how to use Broadcast receiver.
2 comments:
Thx,
I am looking for the same.
Nice and easy thanx
Post a Comment