我在android studio上制作了一个Android应用程序,从那个应用程序我想通过whatsapp向一个号码发送消息。该号码不在我的联系人列表中。我尝试了在stackoverflow上找到的以下代码:
Uri mUri = Uri.parse("smsto:+999999999");
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
mIntent.putExtra("sms_body", "The text goes here");
mIntent.putExtra("chat",true);
startActivity(mIntent);
但它会引发异常“未找到活动”。
我还找到了一个代码,但它打开了我不想要的聊天屏幕意味着用户应该只发送消息发送而不是任何其他 屏幕。
如果有人有解决方案,请告诉我。
答案 0 :(得分:1)
public void onClickWhatsApp(View view) {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info=pm.getPackageInfo("com.whatsapp",
PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
试试这个,或查看此链接sharingwithwhatsapp
答案 1 :(得分:0)
使用此功能可以向任何应用程序上的任何号码发送消息。
public void whatsApp(View v) {
String smsNumber = "97xxxxxxx";
boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
if (isWhatsappInstalled) {
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(smsNumber) + "@s.whatsapp.net");//phone number without "+" prefix
startActivity(sendIntent);
} else {
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
Toast.makeText(this, "WhatsApp not Installed",
Toast.LENGTH_SHORT).show();
startActivity(goToMarket);
}
}
private boolean whatsappInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
答案 2 :(得分:0)
**
**
<强> activity_main.xml中强>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.code2care.example.whatsappintegrationexample.MainActivity"
tools:ignore="HardcodedText" >
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="sendMessae"
android:layout_marginBottom="21dp"
android:text="Send to WhatsApp" />
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:hint="Type your WhatsApp message here ...."
android:layout_alignParentTop="true"
android:background="#FFFFFFEF"
android:padding="10dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:layout_height="200dp"
android:ems="10" />
<强> activity_main.java 强>
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
private EditText message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
message = (EditText) findViewById(R.id.message);
}
public void sendMessage(View v) {
String whatsAppMessage = message.getText().toString();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
sendIntent.setType("text/plain");
// Do not forget to add this to open whatsApp App specifically
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
}
}