2

我有一个 android 应用程序的要求,该应用程序应在以下 2 种情况下自动打开 - 当使用 android 设备的用户到达某个特定位置时。- 当应用程序从服务器收到一些推送通知时。

我是 android 新手,在上述 2 种情况下,应用程序是否有可能自动打开。

4

2 回答 2

0
  1. 对于基于位置的应用程序,请使用 Android 的LocationManger

此类提供对系统位置服务的访问。这些服务允许应用程序获取设备地理位置的定期更新,或在设备进入给定地理位置附近时触发应用程序指定的 Intent。

  1. 对于推送消息使用GCM
于 2013-03-18T14:35:34.483 回答
0

几天前我遇到了这个问题(第二个)并想出了一个“临时”解决方案(可能不是最干净和最酷的)。尽管这个问题很老,但有人可能会遇到同样的问题并在这里找到一些价值。

关于在收到推送通知时如何自动打开 Android 应用程序的第二个问题,从服务类内部输入以下内容:

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

请记住,上面的代码将切断通知声音,因此检查屏幕状态并在锁定时播放一些声音可能是个好主意:

/* 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)
于 2015-04-23T10:30:34.190 回答