Questions tagged [android-broadcast]

A broadcast intent on the Android platform is a special intent which is often invoked by the system on an event and catched by a BroadcastReceiver.

A broadcast intent on the Android platform is a special intent which is often invoked by the system on an event and catched by a BroadcastReceiver.

You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the <receiver> tag in your AndroidManifest.xml.

There are following two important steps to make BroadcastReceiver works for the systen broadcasted intents:

  • Creating the Broadcast Receiver.
  • Registering Broadcast Receiver

Creating the Broadcast Receiver

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter.

public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
   }
}

Registering Broadcast Receiver

An application listens for specific broadcast intents by registering a broadcast receiver in AndroidManifest.xml file. Consider we are going to register MyReceiver for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process.

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >

   <receiver android:name="MyReceiver">
      <intent-filter>
     <action android:name="android.intent.action.BOOT_COMPLETED">
      </action>
      </intent-filter>
   </receiver>
</application>

Now whenever your Android device gets booted, it will be intercepted by BroadcastReceiver MyReceiver and implemented logic inside onReceive() will be executed.

See also the documentation about Intents and Intent Filters.

1235 questions
-3
votes
2 answers

Android how to self start my application when my device powered on?

I am working on android application,i just want to when i powered off my device,when i start again my device my application should self start,is it possible to the android?if yes,please help,any help will be highly appreciated.Thanks. my custom…
Farhan Shah
  • 2,344
  • 7
  • 28
  • 54
-4
votes
1 answer

What's the use of broadcast in android?

I read this article to understand the difference between implicit and explicit broadcast. After going through this I'm getting confused with the concept of broadcast itself. In general, the term broadcast means to scatter/announce the information to…
Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
-4
votes
1 answer

Check the network Connectivity is stable since last one minute

I want to check the connection stability for my project. So kindly help to get time in minute from last Connectivity change i.e.(Connected to notConnected).
Ades rTf
  • 21
  • 1
  • 3
-5
votes
1 answer

Android code help needed

I need to create an app where I have list of songs that are located on a remote server, and the user should be able to download them. Now my problem is how can I track download status(downloading, downloaded) in an example Download manager class,…
Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
-6
votes
2 answers

BroadcastReceiver setting own context

is is possible to set own context after creating broadcastreceiver like this:? public class MyFragment extends Fragment(){ Button myButton; @Override onCreate { myButton = (Button) findview... myButton.setOnClickListner(myListener); } . . …
Nimdokai
  • 787
  • 1
  • 6
  • 18
1 2 3
82
83