使用context.startActivity直接向whatsapp发送消息

时间:2014-12-29 04:45:32

标签: android android-intent code-reuse start-activity

我想通过whatsapp直接发送消息,我发现下面的代码正常工作。但是,在将代码作为常用函数调用移动到另一个java文件后,它无法正常工作并崩溃。那有什么不对?感谢

之前:

Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
            sendIntent.setType("text/plain");
            sendIntent.setPackage("com.whatsapp");
            startActivity(sendIntent);

后:

public void share2whatsapp(Context context) {

    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    sendIntent.setType("text/plain");
    sendIntent.setPackage("com.whatsapp");
    context.startActivity(sendIntent);
}

1 个答案:

答案 0 :(得分:-1)

public void share2whatsapp(Context context) {
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    sendIntent.setType("text/plain");
    sendIntent.setPackage("com.whatsapp");
    sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(sendIntent);
}