我正在尝试在 2.3.3 版的 android 设备上显示谷歌地球
我尝试了以下代码,询问我“安装 Google 地球”,然后将我重定向到 Google Play 商店。但是,它会显示消息“没有找到 pname:com.google.earth 的结果”
我还将 uri 更改为“com.google.earth”和“m.google.earth”,但它们都不起作用。感谢帮助。
这是我的代码。
package com.example.test;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// the new intent we will launch
Intent myIntent = new Intent();
// send the intent directly to the google earth activity that can
// handle search
myIntent.setClassName("com.google.earth",
"com.google.earth.EarthActivity");
// we are doing a search query
myIntent.setAction(Intent.ACTION_SEARCH);
// change this address to any address you want to fly to
myIntent.putExtra(SearchManager.QUERY, "2900 Frenchmen Street, New Orleans, LA");
// always trap for ActivityNotFound in case Google earth is not on the device
try {
// launch google earth and fly to location
this.startActivity(myIntent);
}
catch (ActivityNotFoundException e) {
showGoogleEarthDialog();
}
}
private void showGoogleEarthDialog() {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(this);
downloadDialog.setTitle("Install Google Earth?");
downloadDialog.setMessage("This application requires Google Earth. Would you like to install it?");
downloadDialog.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("market://search?q=pname:com.google.earth"); //pname:
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
MainActivity.this.startActivity(intent);
}
});
downloadDialog.setNegativeButton("No", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {}
});
downloadDialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}