2

I have a requirement in an android app, where the app should open automatically in the following 2 cases -when the user with android device reaches some particular location. -When the app receives some push notification from the server.

Im new to android, Is there any possibility that the app opens up automatically in the above 2 cases.

Rajesh Rs
  • 1,301
  • 5
  • 24
  • 48

2 Answers2

0
  1. For location based app use Android's LocationManger

This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.

  1. For Push message use GCM
Uriel Frankel
  • 14,304
  • 8
  • 47
  • 69
0

I bumped into this problem (the second one) a few days ago and came up with a "temporary" solution (maybe not the cleanest and coolest). Even though this question is old, somebody might have the same problem and find some value here.

Regarding your second question on how you can open and android app automatically when a push notification is received, from inside the service class just put this:

Intent dialogIntent = new Intent(this, myActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);

Keep in mind that the code from above will cut off the notification sound so it might be a good idea to check the Screen state and play some sound if it's locked:

/* Check if the device is locked, play ring tone and vibrator */
if (!(((PowerManager) getSystemService(Context.POWER_SERVICE)).isScreenOn())){
  vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
  Sound = MediaPlayer.create(getApplicationContext(), R.raw.sound);

  try { Sound.start(); }                     
  catch(Exception e){ Log.i("intentService", "Error Playing sound");}

  vibrator.vibrate(3000);

}

dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);

Also, don't forget to release mediaplayer !! just do some research and you'll find an answer, there are different ways but one way could be to wait until the sound is played:

Sound.isPlaying()

And then release:

Sound.release()

P.S: You can set the notificationSound to null and remove the "if" condition so the ringtone will always be played (even when the screen is ON and the app is opened automatically)

setSound(null)
JaimeToca
  • 46
  • 1
  • 5