Skip to content
This repository was archived by the owner on Jun 23, 2022. It is now read-only.

Commit 745a01e

Browse files
author
Lyla
committed
3.12 Add map intent from new menu item
1 parent cdd3f2c commit 745a01e

4 files changed

Lines changed: 47 additions & 5 deletions

File tree

app/src/main/java/com/example/android/sunshine/app/MainActivity.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616
package com.example.android.sunshine.app;
1717

1818
import android.content.Intent;
19+
import android.content.SharedPreferences;
20+
import android.net.Uri;
1921
import android.os.Bundle;
22+
import android.preference.PreferenceManager;
2023
import android.support.v7.app.ActionBarActivity;
24+
import android.util.Log;
2125
import android.view.Menu;
2226
import android.view.MenuItem;
2327

2428
public class MainActivity extends ActionBarActivity {
2529

30+
private final String LOG_TAG = MainActivity.class.getSimpleName();
31+
2632
@Override
2733
protected void onCreate(Bundle savedInstanceState) {
2834
super.onCreate(savedInstanceState);
@@ -54,6 +60,34 @@ public boolean onOptionsItemSelected(MenuItem item) {
5460
return true;
5561
}
5662

63+
if (id == R.id.action_map) {
64+
openPreferredLocationInMap();
65+
return true;
66+
}
5767
return super.onOptionsItemSelected(item);
5868
}
69+
70+
private void openPreferredLocationInMap() {
71+
SharedPreferences sharedPrefs =
72+
PreferenceManager.getDefaultSharedPreferences(this);
73+
String location = sharedPrefs.getString(
74+
getString(R.string.pref_location_key),
75+
getString(R.string.pref_location_default));
76+
77+
// Using the URI scheme for showing a location found on a map. This super-handy
78+
// intent can is detailed in the "Common Intents" page of Android's developer site:
79+
// http://developer.android.com/guide/components/intents-common.html#Maps
80+
Uri geoLocation = Uri.parse("geo:0,0?").buildUpon()
81+
.appendQueryParameter("q", location)
82+
.build();
83+
84+
Intent intent = new Intent(Intent.ACTION_VIEW);
85+
intent.setData(geoLocation);
86+
87+
if (intent.resolveActivity(getPackageManager()) != null) {
88+
startActivity(intent);
89+
} else {
90+
Log.d(LOG_TAG, "Couldn't call " + location + ", no receiving apps installed!");
91+
}
92+
}
5993
}

app/src/main/res/menu/detail.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<menu xmlns:android="http://schemas.android.com/apk/res/android"
22
xmlns:app="http://schemas.android.com/apk/res-auto"
33
xmlns:tools="http://schemas.android.com/tools"
4-
tools:context="com.example.android.sunshine.app.DetailActivity">
5-
<item android:id="@+id/action_settings" android:title="@string/action_settings"
6-
android:orderInCategory="100" app:showAsAction="never" />
4+
tools:context="com.example.android.sunshine.app.DetailActivity" >
5+
<item android:id="@+id/action_settings"
6+
android:title="@string/action_settings"
7+
android:orderInCategory="100"
8+
app:showAsAction="never" />
79
</menu>

app/src/main/res/menu/main.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<menu xmlns:android="http://schemas.android.com/apk/res/android"
22
xmlns:app="http://schemas.android.com/apk/res-auto"
33
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
4-
<item android:id="@+id/action_settings" android:title="@string/action_settings"
5-
android:orderInCategory="100" app:showAsAction="never" />
4+
<item android:id="@+id/action_settings"
5+
android:title="@string/action_settings"
6+
android:orderInCategory="100"
7+
app:showAsAction="never" />
8+
<item android:id="@+id/action_map"
9+
android:title="@string/action_map"
10+
app:showAsAction="never" />
611
</menu>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
typically from the action bar. The ActionBar is limited real estate, so shorter is better.
1414
-->
1515
<string name="action_settings">Settings</string>
16+
<string name="action_map">Map Location</string>
1617

1718
<!-- Menu label to fetch updated weather info from the server -->
1819
<string name="action_refresh" translatable="false">Refresh</string>

0 commit comments

Comments
 (0)