Binding service by BroadcastReceiver

后端 未结 8 1041
春和景丽
春和景丽 2021-02-12 15:32

My Application uses a service that is started by a BOOT_COMPLETE BroadcastReceiver, in run i\'m getting an error

my code:

public class projet extends Br         


        
8条回答
  •  一向
    一向 (楼主)
    2021-02-12 15:57

    Best way is to first start an IntentService from BroadcastReceiver.onReceive() using:

    context.StartService(new Intent(context, YourIntentService.class));
    

    Then in the IntentService.onHandleIntent():

    @Override
    protected void onHandleIntent(Intent intent) {
        mContext = getApplicationContext();
        mContext.bindService(new Intent("com.ServiceToBind.BIND"), yourConnection, Context.BIND_AUTO_CREATE);
    }
    

提交回复
热议问题