user1335611
user1335611

Reputation:

Not able to start Service using Broadcast Receivers

I want to start a service using BroadcastReceiver with this code

public void onReceive(Context context, Intent intent) {

    Intent myIntent = new Intent(context,BlueToothService.class);   

    context.startService(myIntent);

}

But not able to start the service. I also registered service and receiver in manifest.

And I have also one doubt, can we use Broadcast Receiver without activity?

This is my service class

public class BlueToothService extends Service {

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

@Override
public void onStartCommand(Intent intent, int startId) {

    super.onStart(intent, startId);
    Toast.makeText(this, "service Started", Toast.LENGTH_LONG);
    doBluetoothJob();

}

My manifest file looks like this.

<uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BROADCAST_SMS" />

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

    <service
        android:name=".BlueToothService"
        android:enabled="true" >
    </service>

    <receiver android:name="com.simsys.bt.DemoBT" >
    </receiver>

    <intent-filter>
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

Upvotes: 0

Views: 8319

Answers (4)

swiftBoy
swiftBoy

Reputation: 35783

This is working with me i created a Receiver.

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "MyReceiver Started", Toast.LENGTH_SHORT).show();
        Intent myIntent=new Intent(context,MyService.class);        
        context.startService(myIntent);
    }
}

then create a Simple Service

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {      
            return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT).show();
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }
}

don't forget to make Entry for Broadcast Receiver and service in manifest file

<application android:icon="@drawable/icon" android:label="@string/app_name">        
<service
    android:enabled="true"
    android:name=".MyService">
    <intent-filter>
        <action
            android:name = "com.rdc.MyService">
        </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>

now after reboot the emulator Toast will appear.

Upvotes: 6

David Wasser
David Wasser

Reputation: 95568

The manifest file you posted is incorrectly formulated. The <service> and <receiver> tags need to be inside the <application> tag. Like this:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <service
        android:name=".BlueToothService"
        android:enabled="true" >
    </service>
    <receiver android:name="com.simsys.bt.DemoBT" >
    </receiver>
</application>

Upvotes: 1

Charles Harley
Charles Harley

Reputation: 7234

You need to override onStartCommand() not onStart() in your Service subclass. onStart() is depreciated, it should still be called but it would be recommended to override onStartCommand() and see if that works.

Additionally, in your manifest file the service, receiver and intent-filter tags should be children of the application tag. The receiver also needs to declare what intents it will process. e.g.

<receiver android:name="com.simsys.bt.DemoBT" >
    <intent-filter>
        <action android:name="com.simsys.bt.intents.StartService" />
    </intent-filter>    
</receiver>

Upvotes: 0

Ion Aalbers
Ion Aalbers

Reputation: 8030

Try showing your toast with:

Toast.makeText(this, "service Started", Toast.LENGTH_LONG).show();

And you need to give the service declaration in the manifest an specific intentfilter with the intent you want it to listen to like:

<receiver android:name="com.simsys.bt.DemoBT" >
    <intent-filter>
        <action android:name="your.intent.action.definition" />
    </intent-filter>    
</receiver>

Upvotes: 0

Related Questions