通过ActionBar来进行分享的基本流程
1.不重要
1 package com.bwf.a13_actionbar; 2 3 import android.os.Bundle; 4 import android.app.ActionBar; 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.widget.Toast; 10 /** 11 * 1. 显示选项菜单 12 2. 提供标签页的切换方式的导航功能,可以切换多个fragment. 13 3. 提供下拉的导航条目. 14 4. 提供交互式活动视图代替选项条目 15 5. 使用程序的图标作为返回Home主屏或向上的导航操作。 16 * */ 17 public class MainActivity extends Activity { 18 ActionBar actionBar; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 // 获取ActionBar 25 actionBar = getActionBar(); 26 // 设置启用HOME图标,使之可点击:4.0以前默认是可点的,4.0以后默认不可点,如果显示了返回图标则也可点击 27 //actionBar.setHomeButtonEnabled(true); 28 // 设置HOME图标左边出现返回图标:默认是不显示的 29 actionBar.setDisplayHomeAsUpEnabled(true); 30 // 启用或者禁用标题:默认是启用的 31 //actionBar.setDisplayShowTitleEnabled(false); 32 // 左上角图标显示或者不显示:默认是显示 33 //actionBar.setDisplayShowHomeEnabled(false); 34 } 35 36 @Override 37 public boolean onCreateOptionsMenu(Menu menu) { 38 // Inflate the menu; this adds items to the action bar if it is present. 39 getMenuInflater().inflate(R.menu.main, menu); 40 return true; 41 } 42 43 @Override 44 public boolean onOptionsItemSelected(MenuItem item) { 45 // TODO Auto-generated method stub 46 String text = ""; 47 switch (item.getItemId()) { 48 // 监听Home图标 49 case android.R.id.home: 50 text = "返回Home页面"; 51 Intent home = new Intent(Intent.ACTION_MAIN); 52 home.addCategory(Intent.CATEGORY_HOME); 53 startActivity(home); 54 break; 55 case R.id.action_add: 56 text = "添加联系人"; 57 58 startActivity(new Intent(this, AddActivity.class)); 59 break; 60 case R.id.action_delete: 61 text = "删除联系人"; 62 break; 63 case R.id.action_update: 64 text = "更新联系人"; 65 break; 66 case R.id.action_search: 67 text = "查询联系人"; 68 break; 69 70 default: 71 break; 72 } 73 74 Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show(); 75 return super.onOptionsItemSelected(item); 76 } 77 78 @Override 79 protected void onDestroy() { 80 Toast.makeText(getApplicationContext(), "onDestory", Toast.LENGTH_LONG).show(); 81 super.onDestroy(); 82 } 83 }
2. 分享主要代码
1 package com.bwf.a13_actionbar; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.util.Log; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.widget.ShareActionProvider; 10 /** 11 * 通过ActionBar来进行分享的基本流程 12 * 1. 通过Item来获取ActionProvider强转成ShareActionProvider 13 * 2. 创建隐式的Intent对象去删选符合条件的分享平台 14 * 3. provider的setShareIntent去设置分享平台 15 * */ 16 public class AddActivity extends Activity { 17 ShareActionProvider provider; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_add); 22 } 23 24 @Override 25 public boolean onCreateOptionsMenu(Menu menu) { 26 // Inflate the menu; this adds items to the action bar if it is present. 27 getMenuInflater().inflate(R.menu.add, menu); 28 29 // 找到分享的选项 30 MenuItem item_share = menu.findItem(R.id.action_share); 31 // 获取到分享组件 32 provider = (ShareActionProvider) item_share.getActionProvider(); 33 // 通过隐式Intent寻找分享平台 34 provider.setShareIntent(getDefaultShareIntent()); 35 return true; 36 } 37 38 private Intent getDefaultShareIntent() { 39 // 创建隐式Intent对象 40 Intent intent = new Intent(Intent.ACTION_SEND); 41 // 设置Intent分享数据类型 42 intent.setType("text/plain"); 43 // 设置分享内容 44 intent.putExtra(Intent.EXTRA_TEXT, "小清新火线骚扰~"); 45 return intent; 46 } 47 }
3.不重要
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_update" android:orderInCategory="40" android:showAsAction="ifRoom" android:icon="@drawable/address_book_32" android:title="更新"/> <item android:id="@+id/action_delete" android:orderInCategory="30" android:showAsAction="ifRoom" android:icon="@drawable/address_book_close_32" android:title="删除"/> <item android:id="@+id/action_add" android:orderInCategory="20" android:showAsAction="always" android:icon="@drawable/address_book_add_32" android:title="添加"/> <item android:id="@+id/action_search" android:orderInCategory="30" android:showAsAction="always" android:icon="@drawable/address_book_search_32" android:title="查询"/> </menu>
4.关于分享的menu.xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 分享:android.widget.ShareActionProvider --> <item android:id="@+id/action_share" android:orderInCategory="100" android:actionProviderClass="android.widget.ShareActionProvider" android:enabled="true" android:showAsAction="always" android:title="分享"/> </menu>
浙公网安备 33010602011771号