【问题标题】:Making the phone silent on nearing a particular location在靠近特定位置时使手机静音
【发布时间】:2012-06-15 09:03:45
【问题描述】:

我正在制作一个 android 应用程序来显示用户在地图上的位置,以及用户当前的经度和纬度,我还使用反向地理编码来显示 android 设备的地址。现在我计划添加一个功能,如果用户靠近特定位置,手机会自动切换到静音模式,为此我制作了一个功能来检查手机是否已经静音。现在我想再做一个,让手机在靠近特定位置时进入静音模式。

我使手机在靠近特定位置时静音的方法是

public void changeToSilent(Location location){

    if(distanceTo(xxxx)<100)
    {
        if(mPhoneIsSilent==true){
        }
        else
        {
            mPhoneIsSilent = true;
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        }
    }
}

现在我在另一个函数中调用这个方法,也就是......

public void onLocationChanged(位置位置) { Log.d(TAG, "onLocationChanged with location " + location.toString()); Stringtext=String.format("Lat:\t%f\nLong:\t%f\nAlt:\t%f\nBearing:\t%f",location.getLatitude(),location.getLongitude(), location. getAltitude(), location.getBearing()); this.locationText.setText(文本);

    changeToSilent(location);

    try {
      List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); 
      for (Address address : addresses) {
        this.locationText.append("\n" + address.getAddressLine(0));
      }

      int latitude = (int)(location.getLatitude() * 1000000);
      int longitude = (int)(location.getLongitude() * 1000000);

      GeoPoint point = new GeoPoint(latitude,longitude);
      mapController.animateTo(point); 

    } catch (IOException e) {
      Log.e("LocateMe", "Could not get Geocoder data", e);

    }
  }

现在我在 oncreate 方法中调用上述函数。现在假设我知道手机应该静音的地方的纬度和经度。 我想知道的是,在changetosilent方法中我必须写什么来代替xxxx?

【问题讨论】:

  • StackOverflow 用于编程问题。你的问题是什么?而且,您的问题是“我该怎么做?”,您尝试过什么?
  • 请将您的代码发布为对您问题的编辑,而不是 cmets。
  • 我做了以下方法,将手机的模式改为静音。公共无效changeToSilent(位置位置){if(distanceTo(xxxx)
  • 现在假设我有一个我想让手机静音的地方,那么我应该在 changetoSilent 方法中使用什么来代替 'xxxxx'?
  • 好的,蒂姆。抱歉没有看到您的评论。

标签: android


【解决方案1】:

您可以使用AudioManager 更改系统音量。

这是一个例子:

AudioManager am =  (AudioManager) getSystemService(AUDIO_SERVICE); 
//am.setStreamVolume(AudioManager.STREAM_MUSIC,6,0); //<-- use this one to set the volume
am.setRingerMode(AudioManager.RINGER_MODE_SILENT); //<-- to put on mute.

编辑:获取Location对象并使用loc.distanceBetween()

【讨论】:

  • 我面临的问题是我的 distanceTo() 方法。我已经编辑了我的问题,请你再读一遍,如果可能的话,给我的问题一个解决方案。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
相关资源
最近更新 更多