Actionbarsherlock Demo 浅析 :Share Action Providers

Share Action Providers:系统自带的ActionProvider,主要完成分享功能。一般情况下只需要传入一个ShareIntent,下面的事情就交给Share Action Providers来完成。


Activity:

@Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(SampleList.THEME); 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);
        ((TextView)findViewById(R.id.text)).setText(R.string.share_action_providers_content);
        copyPrivateRawResourceToPubliclyAccessibleFile();
    }

copyPrivateRawResourceToPubliclyAccessibleFile()方法主体:

/**
该方法首先都去了一个Raw(原生)图片资源,将它写入文件名叫做SHARED_FILE_NAME:shared.png的文件中,然后将它作为分享的内容传给其他app。
*/
    private void copyPrivateRawResourceToPubliclyAccessibleFile() {
        InputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            inputStream = getResources().openRawResource(R.raw.robot);
            outputStream = openFileOutput(SHARED_FILE_NAME,
                    Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
            byte[] buffer = new byte[1024];
            int length = 0;
            try {
                while ((length = inputStream.read(buffer)) > 0){
                    outputStream.write(buffer, 0, length);
                }
            } catch (IOException ioe) {
                /* ignore */
            }
        } catch (FileNotFoundException fnfe) {
            /* ignore */
        } finally {
            try {
                inputStream.close();
            } catch (IOException ioe) {
               /* ignore */
            }
            try {
                outputStream.close();
            } catch (IOException ioe) {
               /* ignore */
            }
        }
    }
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // 装载自己的menu.
        getSupportMenuInflater().inflate(R.menu.share_action_provider, menu);

        // Set file with share history to the provider and set the share intent.
        MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
        ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
        actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
        // Note that you can set/change the intent any time,
        // say when the user has selected an image.
        actionProvider.setShareIntent(createShareIntent());

        //XXX: 目前, ShareActionProviders 必须要在Actionbar上显示,当然可以定义自己的ActionProvider

        return true;
    }

setShareHistoryFileName(  )方法:用于设置和保存那些分享过数据的目标,比如邮件,QQ空间等。默认情况下null,当你使用过一次之后,下次打开Activity分享内容的时候,就会显示目标的视图或者名字。视图由oncreateactionview()产生,默认值default_share_history_file_name,初始化的时候是null。


createShareIntent()方法主体:

private Intent createShareIntent() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("image/*");
        Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));//以uri的形式传递给share action provider
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        return shareIntent;
    }

最终分享内容是一张图片,名字叫做shared.png。但是因为我使用的是flyme系统,他将图片转变了成了彩信发送,导致文件名变化了。


- - - - - - - - -> 自定义Share Action Provider

案例1:

参考:http://www.tuicool.com/articles/UBbi2i

ShareActionProvider 类有个函数 onCreateActionView, 这个函数会返回分享菜单的View,这个View是可以放到其他界面地方的。所以获取到该View加入界面中即可,如下示例:

ShareActionProvider provider = new ShareActionProvider(this);
        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "Test Text");
        sendIntent.setType("text/plain");
        provider.setShareIntent(sendIntent);
        //获取分享菜单的View
        mShareView = provider.onCreateActionView();
        mShareView.setBackgroundResource(android.R.drawable.dark_header);

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
        int wrap = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
        //添加到布局中
        layout.addView(mShareView, wrap, wrap);

唯一需要注意的就是上面添加菜单到布局中的代码,需要指定宽度和高度参数为WAP_CONTENT,否则的话在4.2.2系统上高度会拉大。

posted @ 2015-08-06 13:19  黑泡man  阅读(213)  评论(0编辑  收藏  举报