Skip to content Skip to sidebar Skip to footer

Calling An Activity From A Service

I have a service class for an alarm service containing the service methods. These methods are called when the alarm service is activated. What I want to do is to call an intent to

Solution 1:

Have you tried what the error log suggests?

i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Solution 2:

You can call an Activity using onStart() of your service.....

@OverridepublicvoidonStart(Intent intent, int startId)  { 
    ...
    Log.i("Service", "onStart() is called"); 
    IntentcallIntent=newIntent(Intent.ACTION_CALL); 
    callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    callIntent.setClass(<Set your package name and classname here>);
    startActivity(callIntent);
    ...

}

Solution 3:

You can do it by enabling the flag as suggested by others. The reason why it is prevented by default is because services are prone to automatic restart by system, in the background. If you are starting an activity during onStart of a service, this activity starts irrespective of what the user may be doing. This will be bad user experience. Please bear this caveat in mind and have work around for this scenario.

Post a Comment for "Calling An Activity From A Service"