Android - New Intent starts particular method

前端 未结 4 1330
心在旅途
心在旅途 2021-01-01 15:09

I want to start one of my existing activities and force the activity to call a specific method after it starts. Is this possible?

Can I define a method that should b

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-01 15:54

    Hello You can't call a particular method from intent but alternately you can use a service from intent and your requirement will be done.

    Intent intent = new Intent(this, MyService.class);

    and in MyService.class

     public class MyService extends Service {
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
          yourParticularMethod();  //do your task and stop the service when your task is done for battery saving purpose
          context.stopService(new Intent(context, MyService.class)); 
    
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    

    }

    and register this service in your manifest file like this

       
    .
    .
    
    

提交回复
热议问题