7

I am implementing the following code, in which I want to start a service using broadcast receiver. The toast in the broadcast receiver is working fine but the service is not executing. Can anyone tell me where I went wrong?

MyReceiver.class
public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        //Toast.makeText(arg0, "Service", Toast.LENGTH_LONG).show();
        Intent myIntent = new Intent(arg0,MyS.class);
        arg0.startService(myIntent);
    }
}


MyS.class
public class MyS extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Service started", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }
}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.p"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
       <service android:enabled="true"
           android:name=".MyS" >
           <intent-filter>
               <action android:name="com.test.p.MyS" >
               </action>
           </intent-filter>
       </service>

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

</manifest>
alrikai
  • 4,123
  • 3
  • 24
  • 23
Spike
  • 71
  • 1
  • 1
  • 2
  • You should register the broadcast for receiving updates and unregister it when not using the activity. `super.registerReceiver(mLoggedOutReceiver, new IntentFilter(LOG_OUT_ACTION));` – nesimtunc Jun 10 '13 at 23:03
  • I have tried that both ways..registering in android manifest and in class Still the service is not starting. Can you send a sample code or link about how to do this? – Spike Jun 12 '13 at 02:14
  • Does the answer here work for you? http://stackoverflow.com/questions/4641712/starting-service-from-broadcastreceiver – Bob Stine Feb 24 '14 at 21:13
  • Does the answer here work for you? http://stackoverflow.com/questions/4641712/starting-service-from-broadcastreceiver – Bob Stine Feb 24 '14 at 21:14

2 Answers2

-1

In your activity, create a BroadcastReceiver variable

    private BroadcastReceiver mBootCompletedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
               // start your service right here... 
        }
    };

and onCreate or onResume events of Activity you should register for that BroadcastReceiver

super.registerReceiver(mBootCompletedReceiver, new IntentFilter("android.intent.action.BOOT_COMPLETED"));

and onDestory or onStop or onPause whatever you in situation should unregister this BroadcastReceiver for not receiving this updates anymore.

super.unregisterReceiver(mBootCompletedReceiver);
Narendra Baratam
  • 828
  • 1
  • 12
  • 26
nesimtunc
  • 849
  • 7
  • 28
-2

The Service will start only when there is an onreceive method called coz u have given the startservice inside the receive method. That means, u have to start some receiver activity like call or sms to start the service. U can instead start service on boot. Google it.

JayadevKV
  • 1
  • 2