+
+ @Override
+ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+ // Inflate the menu; this adds items to the action bar if it is present.
+ inflater.inflate(R.menu.detailfragment, menu);
+
+ // Retrieve the share menu item
+ MenuItem menuItem = menu.findItem(R.id.action_share);
+
+ // Get the provider and hold onto it to set/change the share intent.
+ ShareActionProvider mShareActionProvider =
+ (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
+
+ // Attach an intent to this ShareActionProvider. You can update this at any time,
+ // like when the user selects a new piece of data they might like to share.
+ if (mShareActionProvider != null ) {
+ mShareActionProvider.setShareIntent(createShareForecastIntent());
+ } else {
+ Log.d(LOG_TAG, "Share Action Provider is null?");
+ }
+ }
+
+ private Intent createShareForecastIntent() {
+ Intent shareIntent = new Intent(Intent.ACTION_SEND);
+ shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
+ shareIntent.setType("text/plain");
+ shareIntent.putExtra(Intent.EXTRA_TEXT,
+ mForecastStr + FORECAST_SHARE_HASHTAG);
+ return shareIntent;
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ // Handle action bar item clicks here. The action bar will
+ // automatically handle clicks on the Home/Up button, so long
+ // as you specify a parent activity in AndroidManifest.xml.
+ int id = item.getItemId();
+ if (id == R.id.action_show_location) {
+ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
+ String location = preferences.getString(getString(R.string.pref_location_key),
+ getString(R.string.pref_location_default));
+ Intent intent = new Intent(Intent.ACTION_VIEW);
+ Uri geoLocation;
+ try {
+ geoLocation = Uri.parse("geo:0,0?q=" + URLEncoder.encode(location, "UTF-8"));
+ intent.setData(geoLocation);
+ } catch (UnsupportedEncodingException e) {
+ Log.e(LOG_TAG, "Error ", e);
+ }
+ if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
+ startActivity(intent);
+ }
+
+ }
+ return super.onOptionsItemSelected(item);
+ }