我对 Android Studio 还很陌生,我在我的应用程序中添加了在两个 map 应用程序之间进行选择的可能性。但我想知道如何添加臭名昭著的“始终打开/仅一次”方法。
private void launchNextActivity(String latitude, String longitude, int state) {
for (int i = 0; i < User.getUserCourses().size(); i++) {
if (User.getUserCourses().get(i).getIdCourse() == course.getIdCourse()) {
//A remplacer par l'appel de l'api
Intent intent;
Location location = getLastKnownLocation();
if (location == null) {
Toast.makeText(getApplicationContext(), "Connexion avec le serveur impossible", Toast.LENGTH_SHORT).show();
return;
}
String uri = "google.navigation:q=" + latitude + "+" + longitude;
uri = uri.replace(" ", "+");
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(uri)).setPackage("com.google.android.apps.maps");
if (intent == null) {
findViewById(R.id.cancel_button).setClickable(false);
Snackbar
.make(findViewById(R.id.snackbar_position), "Google Map missing", Snackbar.LENGTH_LONG)
.setAction("Download", new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.apps.maps")));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.maps")));
}
findViewById(R.id.cancel_button).setClickable(true);
}
})
.setActionTextColor(getResources().getColor(R.color.ShGreen2))
.show();
}
//Load Waze if available
Intent wazeIntent;
String uriWaze = "https://waze.com/ul?ll=" + latitude + "," + longitude + "&navigate=yes";
wazeIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uriWaze)).setPackage("com.waze");
Intent chooserIntent = Intent.createChooser(intent, "Ouvrir l'itinéraire avec:");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { wazeIntent });
reloadActivity(state, chooserIntent);
}
我搜索了近一周,几乎没有找到有关其实际工作原理的信息。我知道因为我总是调用选择器,所以它总是会启动它,但是如何在没有选择器的情况下添加“始终打开/仅打开一次”?
最佳答案
您可以使用 geo URI scheme在用户的默认 map 应用程序中打开一个位置(如果未设置默认应用程序,这将导致“始终打开/仅打开一次”对话框)。
Uri location = Uri.parse("geo:" + latitude + "," + longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, location);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
关于java - 如何添加一次always方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47268144/