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

Commit

Permalink
3.12 Add map intent from new menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyla committed Mar 4, 2015
1 parent cdd3f2c commit 745a01e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
Expand Up @@ -16,13 +16,19 @@
package com.example.android.sunshine.app;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

private final String LOG_TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -54,6 +60,34 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
}

if (id == R.id.action_map) {
openPreferredLocationInMap();
return true;
}
return super.onOptionsItemSelected(item);
}

private void openPreferredLocationInMap() {
SharedPreferences sharedPrefs =
PreferenceManager.getDefaultSharedPreferences(this);
String location = sharedPrefs.getString(
getString(R.string.pref_location_key),
getString(R.string.pref_location_default));

// Using the URI scheme for showing a location found on a map. This super-handy
// intent can is detailed in the "Common Intents" page of Android's developer site:
// http://developer.android.com/guide/components/intents-common.html#Maps
Uri geoLocation = Uri.parse("geo:0,0?").buildUpon()
.appendQueryParameter("q", location)
.build();

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(geoLocation);

if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Log.d(LOG_TAG, "Couldn't call " + location + ", no receiving apps installed!");
}
}
}
8 changes: 5 additions & 3 deletions app/src/main/res/menu/detail.xml
@@ -1,7 +1,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.android.sunshine.app.DetailActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
tools:context="com.example.android.sunshine.app.DetailActivity" >
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
9 changes: 7 additions & 2 deletions app/src/main/res/menu/main.xml
@@ -1,6 +1,11 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/action_map"
android:title="@string/action_map"
app:showAsAction="never" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -13,6 +13,7 @@
typically from the action bar. The ActionBar is limited real estate, so shorter is better.
-->
<string name="action_settings">Settings</string>
<string name="action_map">Map Location</string>

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

5 comments on commit 745a01e

@learnitmyway
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I have two questions:

  1. Why are we implementing openPreferredLocationInMap() in MainActivity.java and not ForecstFragement.java?
  2. Why don't we have to consider network/GPS permissions?

@ITJammish
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dswallow - i'll do my best to answer as far as I understand:

  1. We're adding the menu item inside the menu xml for the Activity so we can always see it when running that Activity. Adding it inside the ForecastFragment would mean we would also have to have the logic defined in any other Fragment we load into this Activity for it to always be seen. Think of the main.xml as a root menu that is always present when that Activity is being used, and each fragment you attach has the ability to add to that root and give Fragment specific menu options.
  2. Since we are opening an external maps app, any permission handling is the responsibility for that app since we're not running any code ourselves that requires those permissions.

@yusband
Copy link

@yusband yusband commented on 745a01e Jul 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got a question that why the format for map query can not match that on google official documents for Map Intents?**(geo:0,0?someString)**I just copied the code of openPreferredLocationInMap(),and my map app told me the parameter can not be parsed.
Here is the link below
https://developer.android.com/guide/components/intents-common.html

@francisrod01
Copy link

@francisrod01 francisrod01 commented on 745a01e Nov 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Google Maps in emulator is crashed because not founded my location with the location save in preferences.
Please, how to resolve this call to Google Maps?

@player2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when loading settings (sharedPref.getString) you provide a skey The name of the preference to retrieve. and a defValues Values to return if this preference does not exist. (line 73-75).

You should still be able to open the settings and change the value.

Please sign in to comment.