I am writing a foreground service to receive GPS notifications, send system notifications and make calls. The application has no Activity
attached to it, only a service that is launched from a boot receiver. When I was trying to start the calling activity from within onLocationChanged() of the service, I got:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Fearing the skeptical question, I decided to look at stackOverFlow, where I found these: Calling startActivity() from outside of an Activity context, Android: Make phone call from service, android start activity from service - all suggesting to do this exact thing.
So, my question is: Why is it inadvisable to use this flag (something about the history stack)? Is it OK to do it in my case?
A simplified code:
public class CallService extends Service implements LocationListener {
@Override
public void onCreate() {
super.onCreate();
startForeground(1, NotificationGenerator.getNotification());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public synchronized void onLocationChanged(Location loc) {
String url = "tel:xxxxxxxxxx";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
...
}