Not able to start Service using Broadcast Receivers

笑着哭i 提交于 2020-01-03 06:42:08

问题


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>

回答1:


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.




回答2:


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>



回答3:


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>



回答4:


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>


来源:https://stackoverflow.com/questions/10584064/not-able-to-start-service-using-broadcast-receivers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!