根据当前位置或接收推送通知自动打开Android应用程序

时间:2013-03-18 02:43:13

标签: android push-notification cordova

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

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

2 个答案:

答案 0 :(得分:0)

  1. 对于基于位置的应用,请使用Android的LocationManger
  2.   

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

    1. 使用推送消息GCM

答案 1 :(得分:0)

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

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

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

另外,不要忘记发布媒体播放器!!只是做一些研究,你会找到答案,有不同的方法,但有一种方法可以等到声音播放:

Sound.isPlaying()

然后发布:

Sound.release()

P.S:您可以将notificationSound设置为null并删除" if"条件因此将始终播放铃声(即使屏幕处于开启状态且应用程序自动打开)

setSound(null)