MapLocation.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; // Several Activity lifecycle methods are instrumented to emit LogCat output // so you can follow this class' lifecycle public class MapLocation extends Activity { private final String TAG = "MapLocation"; @Override protected void onCreate(Bundle savedInstanceState) { // Required call through to Activity.onCreate() // Restore any saved instance state super.onCreate(savedInstanceState); // Set content view setContentView(R.layout.main); // Initialize UI elements final EditText addrText = (EditText) findViewById(R.id.location); final Button button = (Button) findViewById(R.id.mapButton); // Link UI elements to actions in code button.setOnClickListener(new OnClickListener() { // Called when user clicks the Show Map button public void onClick(View v) { try { // Process text for network transmission String address = addrText.getText().toString(); address = address.replace(' ', '+'); // Create Intent object for starting Google Maps application Intent geoIntent = new Intent( android.content.Intent.ACTION_VIEW, Uri .parse("geo:0,0?q=" + address)); // Use the Intent to start Google Maps application using Activity.startActivity() startActivity(geoIntent); } catch (Exception e) { // Log any error messages to LogCat using Log.e() Log.e(TAG, e.toString()); } } }); } @Override protected void onStart() { super.onStart(); Log.i(TAG, "The activity is visible and about to be started."); } @Override protected void onRestart() { super.onRestart(); Log.i(TAG, "The activity is visible and about to be restarted."); } @Override protected void onResume() { super.onResume(); Log.i(TAG, "The activity is and has focus (it is now \resumed\ "); } @Override protected void onPause() { super.onPause(); Log.i(TAG, "Another activity is taking focus (this activity is about to be \paused\ "); } @Override protected void onStop() { super.onStop(); Log.i(TAG, "The activity is no longer visible it is now \stopped\ "); } @Override protected void onDestroy() { super.onDestroy(); Log.i(TAG, "The activity is about to be destroyed."); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/location" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:hint="@string/location_string" android:inputType="textPostalAddress" /> <Button android:id="@+id/mapButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/location" android:layout_centerHorizontal="true" android:text="@string/show_map_string" /> </RelativeLayout> |