Introduction

This article explains how to create a global broadcast receiver as well as to create services in an Android project. First of all, we will create a new project in Android.
Procedure
  1. Start the Eclipse IDE.
  2. Create a new project.
  3. Create a MainActivity.java (here Abhijeet.java) file.
  4. Create a new java file bootstartup.java as in the following:
    1. public class bootstartup extends BroadcastReceiver {
    2. @Override
    3. public void onReceive(Context context, Intent intent) {
    4. // start the service
    5. Intent service=new Intent(context,TestService.class);
    6. context.startService(service);
    7. Log.e("com.example.myglobalbroadcastreceiver",
    8. "BroadcastReceived:"+intent.getAction())
    9. // start the app
    10. Intent app= new Intent(context,Abhijeet.class);
    11. context.startService(app);
    1. Create a new java file TestService.java as in the following:
      1. public class TestService extends Service {
      2. @Override
      3. public IBinder onBind(Intent arg0) {
      4. // TODO Auto-generated method stub
      5. throw new UnsupportedOperationException("it is not implemented");
      6. }
      7. @Override
      8. public void onCreate() {
      9. // TODO Auto-generated method stub
      10. super.onCreate();
      11. Toast.makeText(getApplicationContext(), "abhijeet's Service is created",1).show();
      12. }
      13. @Override
      14. public void onDestroy() {
      15. // TODO Auto-generated method stub
      16. Toast.makeText(getApplicationContext(), "Service is destroyed",1).show();
      17. super.onDestroy();
      18. }
      19. @Override
      20. public int onStartCommand(Intent intent, int flags, int startId) {
      21. // TODO Auto-generated method stub
      22. Toast.makeText(getApplicationContext(), " abhijeet's Service is working",1).show();
      23. return super.onStartCommand(intent, flags, startId);
      24. }
      1. Now add a bootstartup.java file as a receiver.
        Step 1: Open the application of the manifest file as in the following:
        Manifest File
        Step 2: Then click on Add for adding a receiver.
        Adding Reciever
        Step 3: It will automatically search the bootstartup receiver class.
        Reciever Class
        Step 4: It is very important for making a global receiver that we should provide a name to that receiver such that the name is started with a dot followed by a lower-case letter.
        Global Reciever
        Step 5: Set the Exported and Enabled properties to true as in the following:
        Enabling Property
        Step 6: Add actions on the receiver in the manifest file as in the following:
        Adding Actions
      2. Now add a TestService.java file as a Service in the manifest file.
        Step 1: Select Service.
        Select Service
        Step 2: It will automatically search the TestService class.
        Adding Class
        Step 3: Select the true option for the exported and enabled properties as in the following:
        1. <service android:name="TestService" android:exported="true" android:enabled="true" android:permission="android.permission.INTERNET"></service>
      Code- Abhijeet.java
      1. package com.example.myglobalbroadcastreceiver;
      2. import android.support.v7.app.ActionBarActivity;
      3. import android.support.v7.app.ActionBar;
      4. import android.support.v4.app.Fragment;
      5. import android.os.Bundle;
      6. import android.view.LayoutInflater;
      7. import android.view.Menu;
      8. import android.view.MenuItem;
      9. import android.view.View;
      10. import android.view.ViewGroup;
      11. import android.os.Build;
      12. public class Abhijeet extends ActionBarActivity {
      13. @Override
      14. protected void onCreate(Bundle savedInstanceState) {
      15. super.onCreate(savedInstanceState);
      16. setContentView(R.layout.activity_abhijeet);
      17. if (savedInstanceState == null) {
      18. getSupportFragmentManager().beginTransaction()
      19. .add(R.id.container, new PlaceholderFragment()).commit();
      20. }
      21. }
      22. @Override
      23. public boolean onCreateOptionsMenu(Menu menu) {
      24. // Inflate the menu; this adds items to the action bar if it is present.
      25. getMenuInflater().inflate(R.menu.abhijeet, menu);
      26. return true;
      27. }
      28. @Override
      29. public boolean onOptionsItemSelected(MenuItem item) {
      30. // Handle action bar item clicks here. The action bar will
      31. // automatically handle clicks on the Home/Up button, so long
      32. // as you specify a parent activity in AndroidManifest.xml.
      33. int id = item.getItemId();
      34. if (id == R.id.action_settings) {
      35. return true;
      36. }
      37. return super.onOptionsItemSelected(item);
      38. }
      39. /**
      40. * A placeholder fragment containing a simple view.
      41. */
      42. public static class PlaceholderFragment extends Fragment {
      43. public PlaceholderFragment() {
      44. }
      45. @Override
      46. public View onCreateView(LayoutInflater inflater, ViewGroup container,
      47. Bundle savedInstanceState) {
      48. View rootView = inflater.inflate(R.layout.fragment_abhijeet,
      49. container, false);
      50. return rootView;
      51. }
      52. }
      53. }
        bootstartup.java
        1. package com.example.myglobalbroadcastreceiver;
        2. import android.content.BroadcastReceiver;
        3. import android.content.Context;
        4. import android.content.Intent;
        5. import android.util.Log;
        6. public class bootstartup extends BroadcastReceiver {
        7. @Override
        8. public void onReceive(Context context, Intent intent) {
        9. // TODO Auto-generated method stub
        10. // start the service
        11. Intent service=new Intent(context,TestService.class);
        12. context.startService(service);
        13. Log.e("com.example.myglobalbroadcastreceiver","Broadcast Received:"+intent.getAction());
        14. // start the app
        15. Intent app= new Intent(context,Abhijeet.class);
        16. context.startService(app);
        17. }
        18. }
          TestService.java
          1. package com.example.myglobalbroadcastreceiver;
          2. import android.app.Service;
          3. import android.content.Intent;
          4. import android.os.IBinder;
          5. import android.widget.Toast;
          6. public class TestService extends Service {
          7. @Override
          8. public IBinder onBind(Intent arg0) {
          9. // TODO Auto-generated method stub
          10. throw new UnsupportedOperationException("it is not implemented");
          11. }
          12. @Override
          13. public void onCreate() {
          14. // TODO Auto-generated method stub
          15. super.onCreate();
          16. Toast.makeText(getApplicationContext(), "abhijeet's Service is created",1).show();
          17. }
          18. @Override
          19. public void onDestroy() {
          20. // TODO Auto-generated method stub
          21. Toast.makeText(getApplicationContext(), "Service is destroyed",1).show();
          22. super.onDestroy();
          23. }
          24. @Override
          25. public int onStartCommand(Intent intent, int flags, int startId) {
          26. // TODO Auto-generated method stub
          27. Toast.makeText(getApplicationContext(), " abhijeet's Service is working",1).show();
          28. return super.onStartCommand(intent, flags, startId);
          29. }
          30. }
            Output
            Step 1: Run the Application.
            Running Application on Android
            Step 2: Click on the back button.
            Clicking on Back Button
            Step 3: Go to Settings.
            Settings on Android App
            Step 4: Go to Data Usage and turn on the mobile data. We can see that the service is working. It is due to the action used in the receiver.
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            Data Usage of Android App