1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.androidx.app; 18 19 import android.content.Intent; 20 import android.view.Menu; 21 22 import androidx.appcompat.app.AppCompatActivity; 23 import androidx.appcompat.widget.ShareActionProvider; 24 import androidx.core.view.MenuItemCompat; 25 26 import com.example.androidx.R; 27 28 /** 29 * This activity demonstrates how to use {@link ShareActionProvider} with the Action Bar. 30 */ 31 public class ActionBarShareActionProvider extends AppCompatActivity { 32 33 @Override onCreateOptionsMenu(Menu menu)34 public boolean onCreateOptionsMenu(Menu menu) { 35 getMenuInflater().inflate(R.menu.action_bar_share_action_provider, menu); 36 return true; 37 } 38 39 @Override onPrepareOptionsMenu(Menu menu)40 public boolean onPrepareOptionsMenu(Menu menu) { 41 final ShareActionProvider sap = (ShareActionProvider) MenuItemCompat.getActionProvider( 42 menu.findItem(R.id.menu_item_share_provider_action_bar)); 43 44 final Intent shareIntent = new Intent(Intent.ACTION_SEND); 45 shareIntent.setType("text/plain"); 46 shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello!"); 47 48 sap.setShareIntent(shareIntent); 49 50 return true; 51 } 52 } 53