How to instantiate android service with a constructor?

后端 未结 6 1950
北海茫月
北海茫月 2021-01-04 09:21

I have a service with an following constructor:

public ShimmerService(Context context, Handler handler) {
    mHandler = handler;
}

I want

6条回答
  •  孤城傲影
    2021-01-04 09:46

    intent service = new Intent(current context, your service name.class);
    `service.putExtra(key,value);`// put your sightly variable type
    `service.setAction("StartDownload");`// action that will detect in onStartCommand
    
     current context.startService(service);
    

    in service, in onStartCommand:

    if (intent.getAction().equals("StartDownload")) {
    `intent.getExtras().getString(key)`;// string in this sample should be your variable type as you used in your code
    //do what you want
        }`
    

提交回复
热议问题