我想在自定义微调器中使用共享按钮

这是我的代码:

btnShareCS_One.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // Share
        // Share Selected Item In Cell-Phone
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String shareBody = txtTitleCS_One.getText().toString() + ": " + edtContentCS_One.getText().toString();
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, txtTitleCS_One.getText().toString());
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
// sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getContext().startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }
});


但不起作用
错误:

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?


如何解决?

我的自定义微调器可以正常工作,代码可以正常运行,但在自定义微调器中无法运行

我尝试使用:

                getContext().startActivity(Intent.createChooser(sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), "Share via"));


所以它也不起作用

最佳答案

用它

            // Share Selected Item In Cell-Phone
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = note.title + ":" + "\n" + note.detail;
            // sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, note.title + ":");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            G.currentActivity.startActivity(Intent.createChooser(sharingIntent, "اشتراک با..."));



    G.currentActivity

G


是全局类扩展应用程序并且

currentActivity


是来自活动的静态变量

10-07 22:09