试图通过短信,whatsapp,蓝牙随机引用并分享意图

时间:2014-09-05 11:17:28

标签: android facebook textview

因此,我开发了一个活动,在单个TextView中显示来自外部文件(quotes.txt)的随机引号。它工作正常。现在,我要做的是在屏幕上显示这个随机引用并通过短信,蓝牙,脸书,whatsapp等分享......

我实现了共享Intent。但它不起作用。事实上,有一些错误。我是新手。这是我的代码

MainActivity.java

public boolean onCreateOptionsMenu (Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setAction(android.content.Intent.ACTION_SEND);

    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, Quotes.add(line));
    startActivity(Intent.createChooser(shareIntent, "Share"));




    return true;
}



}

菜单Main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_share"
    android:orderInCategory="100"
    android:showAsAction="ifRoom"
    android:title="Share"
    android:actionProviderClass="android.widget.ShareActionProvider"/>

1 个答案:

答案 0 :(得分:0)

尝试以下代码

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setAction(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, ""); //<=adding blank quotes
shareIntent.putExtra(Intent.EXTRA_TEXT, Quotes.add(line));    
startActivity(Intent.createChooser(shareIntent, "Share"));

修改

您需要在TextView 活动

中的课程级别定义Main
TextView textView ;
public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //defining view at class level, make it accessible in all method
        //initalize the view in onCreate() instead of updateTextView()
        textView = (TextView)findViewById(R.id.widget_textview1);
}

public boolean onCreateOptionsMenu (Menu menu) {

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setAction(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, ""); //<=adding blank quotes
    shareIntent.putExtra(Intent.EXTRA_TEXT, textView.getText().toString());    
    startActivity(Intent.createChooser(shareIntent, "Share"));
}

updateTextView()方法中,执行此操作

private void updateTextView() {
    List<String> Quotes = getListFromTxtFile("quotes.txt");
    int nextInt = random.nextInt(Quotes.size());
    textView.setText(Quotes.get(nextInt));    
}

接受答案,如果有帮助!!