0

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);
    }

    ...
}
Community
  • 1
  • 1
Elist
  • 5,313
  • 3
  • 35
  • 73

1 Answers1

2

The answer to that lies purely in User Experience (UX) domain. Android devices are usually personal devices and you should put that in mind while coding your app.

Users are maybe playing a game or making a phone call, launching your activity without any notification is rude and I would uninstall any app that would do that.

Also if the phone is locked your activity will not actually launch instead it will wait until the user unlocks the phone.

Notifications on the other hand are made to tell the user that the app wants to show you something. So use them instead.

Unless you are building a private app then you know what is better for your requirements.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
Mr.Me
  • 9,192
  • 5
  • 39
  • 51
  • I would be more specific about the app: it is meant to call and open a gate whenever I drive towards it, using GPS location. I (and everyone else installing this app) expect and except this rude behavior when the phone automatically opens the gate for me, even if it is in the middle of another call (the new call won't be more the 5 sec), or when someone in the back seat is playing a game, because that is exactly what it is meant for. – Elist Feb 25 '13 at 09:23
  • By the way, I would love to hear if you have a solution for the problem you brought up (and which I am going to ask about separately) - How is it possible to keep the service receiving notifications and starting the call activity when the screen is locked. – Elist Feb 25 '13 at 09:24
  • I'm not sure if that is possible via public APIs you may look for hacks for that – Mr.Me Feb 25 '13 at 09:35
  • 1
    don't ask, most likely the question would be marked as duplicate. instead look here: http://stackoverflow.com/search?q=%5Bandroid%5D+screen+lock++ – Mr.Me Feb 25 '13 at 09:42
  • Elist , @Mr.Me is right that user will feel cumbersome when some thing some up in the middle to his/her task and intervention like this should never be flow of apps . You should send notification to notification manager or make sound and vibration etc . and later via pending intent you can start your activity – Vipin Sahu Feb 25 '13 at 09:43
  • @VipinSahu, the whole idea of the app is to work when the phone is unreachable. I might add a sound interface with the device later, but for now it is the best why to accomplish what I'm trying to do. – Elist Feb 25 '13 at 09:49
  • you can send a message in response to alert the user , where mesaage can be send in back ground and can contain lat lng , if u need ..... just a suggestion- @Elist – Vipin Sahu Feb 25 '13 at 10:02