Per avviare Google Maps con un intent,andiamo a creare un oggetto Intent specificandone l’azione con ACTION_VIEW e lo passiamo al metodo startActivity(). Il sistema avvierà Google Maps, e l’Activity corrispondente.
Ecco il codice:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”
android:orientation=”vertical”>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceMedium”/>
<Button
android:id=”@+id/map”
android:text=”MapView”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”/>
</LinearLayout>
package com.example.openMap.activities;
import android.app.Activity;
import android.os.Bundle;
import com.example.openMap.R;
import android.widget.*;
import android.view.*;
import android.content.*;
public class MainActivity extends Activity {
Button btn;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.map);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(“com.google.android.apps.maps”);
startActivity(intent);
}
});
}
}
Manifest:
<uses-permission android:name=”android.permission.INTERNET” />