我已经使用该代码在 30 秒间隔和 30 米距离后播放 mp3 声音,但在手机处于睡眠状态并且我已经行驶了 5 英里之后它不会播放。
如何使上面的代码工作?请帮忙。
提前致谢。
我有一个插件查询:onLocationChanged 方法是否知道我的最后位置?换句话说,如果我执行了这个语句:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30, 30000, new MyLocationListener());
mp3 文件会每 30 秒间隔和 30 米的距离自行执行吗?或者我是否必须像这样在 ScheduledExecutorService 中执行语句:
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleWithFixedDelay(new Runnable(){
public void run() {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30, 30000, new MyLocationListener());
}
}, 0, 600, TimeUnit.SECONDS);
总代码:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30, 30000, new MyLocationListener());
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
///Audio file which plays everytime the above condition is met
mp3.start();
);
Toast.makeText(AndroidGPSSampleActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(AndroidGPSSampleActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(AndroidGPSSampleActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
System.out.println("==onProviderEnabled=" + s);
Toast.makeText(AndroidGPSSampleActivity.this, "Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
}