Android: how to get the intent received by a service?

前端 未结 3 1954
情深已故
情深已故 2020-12-29 02:28

I\'m starting a service with an intent where I put extra information.

How can I get the intent in the code of my service?

There isn\'t a function like

相关标签:
3条回答
  • 2020-12-29 03:19

    onStart() is deprecated now. You should use onStartCommand(Intent, int, int) instead.

    0 讨论(0)
  • 2020-12-29 03:23

    Override onStart() -- you receive the Intent as a parameter.

    0 讨论(0)
  • 2020-12-29 03:30

    To pass the extras:

    Intent intent = new Intent(this, MyService.class);
    intent.putExtra(MyService.NAME, name);
    ...
    startService(intent);
    

    To retrieve the extras in the service:

    public class MyService extends Service {  
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            super.onStartCommand(intent, flags, startId);
            String name = intent.getExtras().getString(NAME);
            ...
        } 
        ...
    } 
    
    0 讨论(0)
提交回复
热议问题