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
- Start the Eclipse IDE.
- Create a new project.
- Create a MainActivity.java (here Abhijeet.java) file.
- Create a new java file bootstartup.java as in the following:
- public class bootstartup extends BroadcastReceiver {
-
- @Override
-
- public void onReceive(Context context, Intent intent) {
-
-
- Intent service=new Intent(context,TestService.class);
- context.startService(service);
- Log.e("com.example.myglobalbroadcastreceiver",
- "BroadcastReceived:"+intent.getAction())
-
-
- Intent app= new Intent(context,Abhijeet.class);
- context.startService(app);
- Create a new java file TestService.java as in the following:
- public class TestService extends Service {
-
- @Override
- public IBinder onBind(Intent arg0) {
-
- throw new UnsupportedOperationException("it is not implemented");
- }
-
- @Override
- public void onCreate() {
-
- super.onCreate();
- Toast.makeText(getApplicationContext(), "abhijeet's Service is created",1).show();
-
- }
-
- @Override
- public void onDestroy() {
-
-
- Toast.makeText(getApplicationContext(), "Service is destroyed",1).show();
-
- super.onDestroy();
- }
-
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
-
-
- Toast.makeText(getApplicationContext(), " abhijeet's Service is working",1).show();
-
- return super.onStartCommand(intent, flags, startId);
- }
- Now add a bootstartup.java file as a receiver.
Step 1: Open the application of the manifest file as in the following:
Step 2: Then click on Add for adding a receiver.
Step 3: It will automatically search the bootstartup receiver 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.
Step 5: Set the Exported and Enabled properties to true as in the following:
Step 6: Add actions on the receiver in the manifest file as in the following:
- Now add a TestService.java file as a Service in the manifest file.
Step 1: Select Service.
Step 2: It will automatically search the TestService class.
Step 3: Select the true option for the exported and enabled properties as in the following:
- <service android:name="TestService" android:exported="true" android:enabled="true" android:permission="android.permission.INTERNET"></service>
Code- Abhijeet.java
- package com.example.myglobalbroadcastreceiver;
- import android.support.v7.app.ActionBarActivity;
- import android.support.v7.app.ActionBar;
- import android.support.v4.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.os.Build;
-
- public class Abhijeet extends ActionBarActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_abhijeet);
-
- if (savedInstanceState == null) {
- getSupportFragmentManager().beginTransaction()
- .add(R.id.container, new PlaceholderFragment()).commit();
- }
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
-
- getMenuInflater().inflate(R.menu.abhijeet, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
-
-
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
-
-
-
-
- public static class PlaceholderFragment extends Fragment {
-
- public PlaceholderFragment() {
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_abhijeet,
- container, false);
- return rootView;
- }
- }
- }
bootstartup.java
- package com.example.myglobalbroadcastreceiver;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
- public class bootstartup extends BroadcastReceiver {
-
- @Override
-
- public void onReceive(Context context, Intent intent) {
-
-
- Intent service=new Intent(context,TestService.class);
- context.startService(service);
- Log.e("com.example.myglobalbroadcastreceiver","Broadcast Received:"+intent.getAction());
-
-
- Intent app= new Intent(context,Abhijeet.class);
- context.startService(app);
- }
- }
TestService.java
- package com.example.myglobalbroadcastreceiver;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.widget.Toast;
-
- public class TestService extends Service {
-
- @Override
- public IBinder onBind(Intent arg0) {
-
- throw new UnsupportedOperationException("it is not implemented");
- }
-
- @Override
- public void onCreate() {
-
- super.onCreate();
- Toast.makeText(getApplicationContext(), "abhijeet's Service is created",1).show();
- }
-
- @Override
- public void onDestroy() {
-
- Toast.makeText(getApplicationContext(), "Service is destroyed",1).show();
- super.onDestroy();
- }
-
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
-
- Toast.makeText(getApplicationContext(), " abhijeet's Service is working",1).show();
- return super.onStartCommand(intent, flags, startId);
- }
- }
Output
Step 1: Run the Application.
Step 2: Click on the back button.
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" />