Android(java)学习笔记38:Android 短信发送

1.第一种可以调用系统内部的短信程序.

之前我曾经出现过一个bug就是报错:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO dat=Tel:xxx }……

这是因为我使用intent调用系统编辑短信服务参数没有设置好,后认真核对修改之后,改成如下的样子就没有问题了:

出现这个报错的原因是:

之前我调用Intent启动activity的代码是:

 Intent intent = new Intent(Intent.ACTION_SENDTO,Uri.parse("sms:“+message);
startActivity(intent);

这里调用这个构造方法是不行的,我们回去看API,发现:

Intent(String action, Uri uri)
          Create an intent with a given action and for a given data url.(这要求是给定的数据,也就是常量),也就是说构造方法参数不能是变量

(具体方法实体,可以去看API)

下面使用intent调用系统内部的短信编辑程序

1 Intent intent = new Intent();
2 intent.setAction(Intent.ACTION_SENDTO);
3 intent.setData(Uri.parse("smsto:"+num));
4 intent.putExtra("sms_body", message);
5 startActivity(intent);

 

Android(java)学习笔记38:Android 短信发送

点击一下:发送短信,程序会主动重新跳转到下面的界面,是调用android系统内部的短信编辑程序

Android(java)学习笔记38:Android 短信发送

2. 就是我们不要调用android系统的短信编辑程序,而是编写属于自己短信编辑程序:(程序部分重要代码如下):

 sendmess_button.setOnClickListener(new View.OnClickListener() {

             @Override
public void onClick(View v) {
String str2 = "";//切记这里只能定义为"",空字符,之前我定义为null,总是无法弹出发送内容为空的提示
String num = edit_num.getText().toString();
String message = edit_mess.getText().toString();
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> texts= smsManager.divideMessage(message);//拆分短信,字数多的短信拆分成多个短信 if (message.equals(str2) || num.equals(str2)) {
Toast.makeText(MainActivity.this.getApplicationContext(),
"你个二愣子,你还没输入短信内容或者输入手机号码", 1).show();
}
// Intent intent = new Intent();
// intent.setAction(Intent.ACTION_SENDTO);
// intent.setData(Uri.parse("smsto:"+num));
// intent.putExtra("sms_body", message);
// startActivity(intent); for (String text : texts) {
smsManager.sendTextMessage(num, null, text, null, null);
} }
});

 在这之后我也遇到Intent调用拨号出现相应错误:

 06-04 04:09:24.285: W/dalvikvm(12398): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-04 04:09:24.326: E/AndroidRuntime(12398): FATAL EXCEPTION: main
06-04 04:09:24.326: E/AndroidRuntime(12398): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.CALL dat=Tel:xxx }
06-04 04:09:24.326: E/AndroidRuntime(12398): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
06-04 04:09:24.326: E/AndroidRuntime(12398): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
06-04 04:09:24.326: E/AndroidRuntime(12398): at android.app.Activity.startActivityForResult(Activity.java:3370)
06-04 04:09:24.326: E/AndroidRuntime(12398): at android.app.Activity.startActivityForResult(Activity.java:3331)
06-04 04:09:24.326: E/AndroidRuntime(12398): at android.app.Activity.startActivity(Activity.java:3566)
06-04 04:09:24.326: E/AndroidRuntime(12398): at android.app.Activity.startActivity(Activity.java:3534)
06-04 04:09:24.326: E/AndroidRuntime(12398): at com.himi.sendmessage.MainActivity$1.onClick(MainActivity.java:47)
06-04 04:09:24.326: E/AndroidRuntime(12398): at android.view.View.performClick(View.java:4204)
06-04 04:09:24.326: E/AndroidRuntime(12398): at android.view.View$PerformClick.run(View.java:17355)
06-04 04:09:24.326: E/AndroidRuntime(12398): at android.os.Handler.handleCallback(Handler.java:725)
06-04 04:09:24.326: E/AndroidRuntime(12398): at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 04:09:24.326: E/AndroidRuntime(12398): at android.os.Looper.loop(Looper.java:137)
06-04 04:09:24.326: E/AndroidRuntime(12398): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-04 04:09:24.326: E/AndroidRuntime(12398): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 04:09:24.326: E/AndroidRuntime(12398): at java.lang.reflect.Method.invoke(Method.java:511)
06-04 04:09:24.326: E/AndroidRuntime(12398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-04 04:09:24.326: E/AndroidRuntime(12398): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-04 04:09:24.326: E/AndroidRuntime(12398): at dalvik.system.NativeStart.main(Native Method)
06-04 04:09:26.744: I/Process(12398): Sending signal. PID: 12398 SIG: 9

这个和上面短信问题一样的,还是基础概念不扎实

上一篇:Win7 64位操作系统中搭建C/C++的编译环境


下一篇:Error message when you try to modify or to delete an alternate access mapping in Windows SharePoint Services 3.0: "An update conflict has occurred, and you must re-try this action"