それについては、stackoverflow に関する多くの投稿を読みましたが、必要な答えが見つかりませんでした。GPS やネットワーク プロバイダーを使用すると、自分の位置を簡単に見つけることができますが、問題があります。私のアプリは最初にGPSがアクティブかどうかを確認し、そうでない場合はユーザーをGPSオプションページに送ります。その後、ユーザーは最初のアクティビティに戻ることができます。このアクティビティと 2 番目のアクティビティで現在地を使用します。最初のアクティビティと関数 "onLocationUpdate()" にのみリスナーを配置し、緯度と経度をグローバル アプリケーション変数に保存します。アプリを起動して GPS を有効に設定し、2 番目のアクティビティを開くと、緯度と経度は 0.0 に等しくなりますが、アクティビティを開始してから GPS を有効に設定し、30 秒待ってから動作します。この遅延を解消する方法はありますか? Network_Provider を使用すると高速ですが、GPS のように正確ではありません!
コードの断片は次のとおりです。
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean gpsStatus = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsStatus) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
alertDialogBuilder.setTitle("The GPS is off!");
alertDialogBuilder
.setMessage("Turn on the GPS?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Intent intent1 = new Intent();
intent1.setClassName(
"com.android.settings",
"com.android.settings.SecuritySettings");
startActivity(intent1);
dialog.cancel();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, locationListener);
この部分は、GPS が有効になっているかどうかを確認するためのものです。設定ページに行かずにGPSを有効にする方法はありますか?
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) { // update
if (location != null) {
msg_r.setLatitude(location.getLatitude());
msg_r.setLongitude(location.getLongitude());
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
これが私のリスナーです。