英文:
open specific whatsapp contact from my android app
问题
我想通过点击按钮在我的WhatsApp中打开特定联系人,我正在使用这段代码,但它无法正常工作。
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("smsto:" + "923000000000");
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(i);
}
});
请指导我。
英文:
I want to open a specific contact in my whatsapp by clicking a button I am using this code but it is not working.
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("smsto:"+"923000000000");
Intent i = new Intent(Intent.ACTION_SENDTO,uri);
i.setPackage("com.whatsapp");
startActivity(i);
Please guide me.
答案1
得分: 3
String contact = "+92 3122804640"; // 使用国家代码和您的电话号码
String url = "https://api.whatsapp.com/send?phone=" + contact;
try {
PackageManager pm = getApplicationContext().getPackageManager();
pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(MainActivity.this, "您的手机未安装WhatsApp应用", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
英文:
Kindly use below code to achieve this.
String contact = "+92 3122804640"; // use country code with your phone number
String url = "https://api.whatsapp.com/send?phone=" + contact;
try {
PackageManager pm = getApplicationContext().getPackageManager();
pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(MainActivity.this, "Whatsapp app not installed in your phone", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
答案2
得分: 1
我正在使用这个:
String phone = "+8801510101010";
PackageManager packageManager = MainActivity.this.getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);
try {
String url = "https://api.whatsapp.com/send?phone=" + phone;
i.setPackage("com.whatsapp");
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null) {
MainActivity.this.startActivity(i);
}
} catch (Exception e){
e.printStackTrace();
}
英文:
I am Using This:
String phone = "+8801510101010";
PackageManager packageManager = MainActivity.this.getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);
try {
String url = "https://api.whatsapp.com/send?phone="+ phone;
i.setPackage("com.whatsapp");
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null) {
MainActivity.this.startActivity(i);
}
} catch (Exception e){
e.printStackTrace();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论