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>
This is working with me i created a Receiver.
then create a Simple Service
don’t forget to make Entry for Broadcast Receiver and service in manifest file
now after reboot the emulator Toast will appear.