Introduction
- 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) {
- // start the service
- Intent service=new Intent(context,TestService.class);
- context.startService(service);
- Log.e("com.example.myglobalbroadcastreceiver",
- "BroadcastReceived:"+intent.getAction())
- // start the app
- 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) {
- // TODO Auto-generated method stub
- throw new UnsupportedOperationException("it is not implemented");
- }
- @Override
- public void onCreate() {
- // TODO Auto-generated method stub
- super.onCreate();
- Toast.makeText(getApplicationContext(), "abhijeet's Service is created",1).show();
- }
- @Override
- public void onDestroy() {
- // TODO Auto-generated method stub
- Toast.makeText(getApplicationContext(), "Service is destroyed",1).show();
- super.onDestroy();
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // TODO Auto-generated method stub
- 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>
- <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) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.abhijeet, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- /**
- * A placeholder fragment containing a simple view.
- */
- 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) {
- // TODO Auto-generated method stub
- // start the service
- Intent service=new Intent(context,TestService.class);
- context.startService(service);
- Log.e("com.example.myglobalbroadcastreceiver","Broadcast Received:"+intent.getAction());
- // start the app
- 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) {
- // TODO Auto-generated method stub
- throw new UnsupportedOperationException("it is not implemented");
- }
- @Override
- public void onCreate() {
- // TODO Auto-generated method stub
- super.onCreate();
- Toast.makeText(getApplicationContext(), "abhijeet's Service is created",1).show();
- }
- @Override
- public void onDestroy() {
- // TODO Auto-generated method stub
- Toast.makeText(getApplicationContext(), "Service is destroyed",1).show();
- super.onDestroy();
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // TODO Auto-generated method stub
- 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.



<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />


Abhijeet SinghPosted Jul 9, 2014, 8:13 AM
Thank you .. :)
Gaurav KataraPosted Jul 9, 2014, 7:47 AM
well explained... nice article... (y)