[英]How can i send SMS from my android app?
I am trying to send an SMS from my android app. 我正在尝试从我的Android应用发送短信。 Every time I click on my button, it goes to catch
part rather then try
. 每次单击按钮时,它都会catch
而不是try
。
Here is my method: 这是我的方法:
Button sms = (Button)findViewById(R.id.button2);
sms.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.i("Send SMS", "");
Intent sms = new Intent(Intent.ACTION_VIEW);
sms.setData(Uri.parse("smsto:"));
sms.setType("vnd.android-dir/mms-sms");
sms.putExtra("address", new String(con));
sms.putExtra("sms_body", "Test SMS to Angilla");
try {
startActivity(sms);
finish();
Log.i("Finished sending SMS...", "");
}
catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(DetailActivity.this,
"SMS faild, please try again later.", Toast.LENGTH_SHORT).show();
}
}
});
You can send sms two ways: 1. SMS Manager 2. Built in app 您可以通过两种方式发送短信:1. SMS Manager 2.内置应用
Both ways need permissions in manifest 两种方式都需要清单中的权限
<uses-permission android:name="android.permission.SEND_SMS" />
Example with sms manager: 短信管理器示例:
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);
Example with built-in app: 内置应用程序示例:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
This is working for me : 这为我工作:
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(Uri.parse("smsto:"));
sendIntent.putExtra("sms_body", "Your message");
try {
startActivity(sendIntent);
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.