Welcome to ANDROID Developer!

Friday, May 23, 2014

Start Activity from Broadcast Recevier


By on 5:09 AM

Some time we need to start an Activity from Broadcast Receiver..
how can we achieve this i am going to write step by step.

So lets create a small App to do this things 

-------------------------------------------
App Name: BReceiver2Activity
Package Name: com.sunil
Android SDK: Android SDK 2.2 / API 8
-------------------------------------------

MyReceiver.java
  1. package com.sunil;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.util.Log;  
  7. import android.widget.Toast;  
  8.   
  9. public class MyReceiver extends BroadcastReceiver {  
  10.   
  11.  @Override  
  12.  public void onReceive(Context context, Intent intent) {  
  13.     
  14.   Toast.makeText(context, "MyReceiver Started",   
  15.     Toast.LENGTH_SHORT).show();  
  16.   Log.v("Info Message""in Broadcast receiver");  
  17.   Intent myIntent=new Intent(context,MyActivity.class);   
  18.   myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  19.   context.startActivity(myIntent);  
  20.  }  
  21.   
  22. }  

MyActivity.java
  1. package com.sunil;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class MyActivity extends Activity {  
  7.   
  8.  @Override  
  9.  public void onCreate(Bundle savedInstanceState) {  
  10.   super.onCreate(savedInstanceState);  
  11.   setContentView(R.layout.main);  
  12.   
  13.  }  
  14. }  

main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.  xmlns:android="http://schemas.android.com/apk/res/android"  
  4.  android:orientation="vertical"  
  5.  android:layout_width="fill_parent"  
  6.  android:layout_height="fill_parent"  
  7.  android:weightSum="1">  
  8.  <TextView  
  9.   android:layout_width="fill_parent"  
  10.   android:layout_height="wrap_content"  
  11.   android:text="@string/hello"  
  12.   android:layout_weight="0.14" />  
  13. </LinearLayout>  

AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest  
  3.  xmlns:android="http://schemas.android.com/apk/res/android"  
  4.  package="com.sunil"  
  5.  android:versionCode="1"  
  6.  android:versionName="1.0">  
  7.  <uses-sdk android:minSdkVersion="8" />  
  8.   
  9.  <application  
  10.   android:icon="@drawable/icon"  
  11.   android:label="@string/app_name">  
  12.   
  13.   <activity  
  14.    android:enabled="true"  
  15.    android:name=".MyActivity">  
  16.    <intent-filter>  
  17.     <action android:name="com.sunil.MyActivity">  
  18.     </action>  
  19.    </intent-filter>  
  20.   </activity>  
  21.   
  22.   <receiver  
  23.     android:enabled="true"  
  24.     android:name=".MyReceiver">  
  25.     <intent-filter>  
  26.     <action android:name="android.intent.action.BOOT_COMPLETED" />  
  27.     </intent-filter>  
  28.   </receiver>  
  29.   
  30.  </application>  
  31. </manifest>  

Now Reboot Emulator, Activity will appear on start-up.

You can download source code here: BReceiver2Activity

About Theavuth NHEL

Faizan is a 17 year old young guy who is blessed with the art of Blogging,He love to Blog day in and day out,He is a Website Designer and a Certified Graphics Designer.

0 comments:

Post a Comment