加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在我的应用程序中使用相同的意图选择器共享多条消息

发布时间:2020-12-15 02:20:30 所属栏目:Java 来源:网络整理
导读:我有这个代码 Intent sharingIntent = new Intent(Intent.ACTION_SEND);if (Build.VERSION.SDK_INT Build.VERSION_CODES.LOLLIPOP) sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);else sharingIntent.addFlags(Intent.FLAG_ACTIVITY_
我有这个代码

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) 
   sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
else  
   sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);

String activityTitle = cellsList.get(position).getTitle(); 
sharingIntent.putExtra(Intent.EXTRA_SUBJECT,activityTitle); 
sharingIntent.putExtra(Intent.EXTRA_STREAM,uri);                                                               
sharingIntent.setType("image/*");
sharingIntent.setPackage("com.whatsapp");
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Intent chooser = Intent.createChooser(sharingIntent,getResources().getString(R.string.share_using));
getActivity().startActivity(chooser);

我想与同一个选择器应用程序共享包含文本和图像的另一条消息.

您对如何解决问题有任何想法吗?

解决方法

您可以使用ShareActionProvider.在下面的示例中,我使用的是一个绑定到工具栏中的MenuItem,但也可以将它们与例如工具栏一起使用.一个按钮.

main_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_item_share"
        app:showAsAction="ifRoom"
        android:title="Share"
        app:actionProviderClass=
            "android.support.v7.widget.ShareActionProvider" />

</menu>

activity_main.xml中

<FrameLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sharing.datasender.MainActivity">

    <TextView
        android:id="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center_vertical"
        android:text="Send data once more"
        />

</FrameLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity
{
    private ShareActionProvider shareActionProvider;
    private Intent shareIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view = findViewById(R.id.view2);
        view.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                sendDataOnceMore();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        MenuItem item = menu.findItem(R.id.menu_item_share);
        shareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(item);
        setShareIntent(getShareIntent());
        shareActionProvider.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener()
        {
            @Override
            public boolean onShareTargetSelected(ShareActionProvider shareActionProvider,Intent intent)
            {
                ComponentName component = intent.getComponent();
                shareIntent = intent;
                String className = null;
                if (component != null)
                {
                    className = component.getClassName();
                }
                Log.d("TEST","onShareTargetSelected: *" + className + "*");
                return false;
            }
        });
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();
        switch (id)
        {
            case R.id.menu_item_share:
                Intent intent = getShareIntent();
                setShareIntent(intent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private Intent getShareIntent()
    {
        Intent sharingIntent = new Intent();
        sharingIntent.setAction(Intent.ACTION_SEND);
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT,"This is my text to send.");
        Uri uri = Uri.parse(IMAGE_PATH);
        sharingIntent.putExtra(Intent.EXTRA_STREAM,uri);
        sharingIntent.setType("image/*");
        sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        sharingIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        return sharingIntent;
    }

    private void sendDataOnceMore()
    {
        if (shareIntent != null)
        {
            Intent sendIntent = new Intent();
            try
            {
                sendIntent.fillIn(shareIntent,Intent.FILL_IN_COMPONENT + Intent.FILL_IN_ACTION + Intent.FILL_IN_CATEGORIES + Intent.FILL_IN_PACKAGE);
                sendIntent.putExtra(Intent.EXTRA_TEXT,"And another thing...");
                sendIntent.setType("text/plain");

                sendIntent.setAction(Intent.ACTION_SEND);
                startActivity(sendIntent);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                Toast.makeText(this,"Boooom!",Toast.LENGTH_LONG).show();
                return;
            }
        }
        else
        {
            Toast.makeText(this,"No Go :(",Toast.LENGTH_LONG).show();
            return;
        }

    }

    // Call in onCreateOptionsMenu() or the MenuItem will not be clickable!
    // Call later to update the share Intent
    private void setShareIntent(Intent shareIntent)
    {
        if (shareActionProvider != null)
        {
            shareActionProvider.setShareIntent(shareIntent);
        }
    }
}

请记住确保sendIntent.resolveActivity()!= null如果您稍后使用它 – 其他应用程序可能已在此期间卸载!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读