Start Activity from Broadcast Recevier


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

cheers!!

I'd love to hear your thoughts!!

18 comments:

  1. Wonderful...Sunil..Can you email your mobile number to viswanath.subr@gmail.com? I am working on an App and struggling to find a solution. It is related to broadcast recievr calling activity..pls hlp

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. The blog gave me idea about the broadcast receiver My sincere thanks for sharing this post
    Android Training in Chennai

    ReplyDelete
  4. Very nice blog. I appreciate your coding knowledge. This blog gave me a good idea to develope the android application.Thanks for sharing


    Android Training in Chennai

    ReplyDelete
  5. Hi, this is a very useful post. your blog's help me to improve android knowledge and i learnt something new from your blog share. thank you. keep sharing...Android Training in Chennai

    ReplyDelete
  6. Hi dude, In this quality information for Activity Broadband of server to estimate certainly for helpful infamous in terrific blog article blog content.I agree to that our New useful point-of-view.Your blog content helps to us for Android broadband informative.If want to become a learn for Dot Net Training Development to have click for below links , Dot Net Training in Chennai
    

    ReplyDelete
  7. really nice blog has been shared by you. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blog.
    android training in chennai

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.


    Android Online Training

    ReplyDelete
  10. Interesting post! This is really helpful for me. I like it! Thanks for sharing!
    Mobile application developers in Chennai | PHP developers Chennai

    ReplyDelete
  11. Extraordinary blog. you put Good stuff. All the themes were clarified briefly.Thanks for sharing that post.Keep in blogging...No.1 IOS Training Institute in Velachery | Best Android Training Institute in Velachery | Core Java Training Institute in Chennai

    ReplyDelete
  12. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    Java Training Center in Chennai | Best J2EE Training Center in Chennai | No.1 Java Training Institution in Velachery | Core Java Training in Chennai

    ReplyDelete
  13. It is most knowledgeable information like this.I will read this article it is very easy to learn this blog.
    No.1 IOS Training Institute in Velachery | Best Android Training Institute in Velachery | Core Java Training Institute in Chennai

    ReplyDelete
  14. Informative blog... Thanq for sharing your thoughts with us ... Nice work .. keep going

    Selenium training | Selenium training in chennai

    ReplyDelete

Debug Database in Android

In my last tutorial, we have learned how we can use persistence database in android. We also understood which database wrapper we can use...

Contact Me

Name

Email *

Message *