第一次尝试短信。从一本 wrox 书中复制代码并且它有效,但是现在当我尝试插入输入自定义数字和自定义文本的功能时,我遇到了一个我无法弄清楚的错误(无效的目标地址......但我从一个模拟器到另一个与该示例一起工作的模拟器......当我输入数字时失败了)。以下是主要活动:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.telephony.SmsManager;
public class SMSActivity extends Activity {
public String contactNumber;
public String messageText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText contact_Number = (EditText)findViewById(R.id.contact_number);
contactNumber = contact_Number.getText().toString();
EditText text_Message = (EditText)findViewById(R.id.text_message);
messageText = text_Message.getText().toString();
}
public void onClick(View v) {
sendSMS(contactNumber, messageText);
}
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}
这是 XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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=".SMSActivity" >
<EditText
android:id="@+id/contact_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" >
<EditText
android:id="@+id/text_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/btnSendSMS" />
<Button
android:id="@+id/btnSendSMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="send"
android:onClick="onClick" />
</RelativeLayout>
</RelativeLayout>
如果您需要,这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<uses-permission
android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="net.learn2develop.sms.SMSActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我点击发送之前的样子:
最后,logcat:
05-11 17:31:05.313: E/AndroidRuntime(589): FATAL EXCEPTION: main
05-11 17:31:05.313: E/AndroidRuntime(589): java.lang.IllegalStateException: Could not execute method of the activity
05-11 17:31:05.313: E/AndroidRuntime(589): at android.view.View$1.onClick(View.java:3039)
05-11 17:31:05.313: E/AndroidRuntime(589): at android.view.View.performClick(View.java:3480)
05-11 17:31:05.313: E/AndroidRuntime(589): at android.view.View$PerformClick.run(View.java:13983)
05-11 17:31:05.313: E/AndroidRuntime(589): at android.os.Handler.handleCallback(Handler.java:605)
05-11 17:31:05.313: E/AndroidRuntime(589): at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 17:31:05.313: E/AndroidRuntime(589): at android.os.Looper.loop(Looper.java:137)
05-11 17:31:05.313: E/AndroidRuntime(589): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-11 17:31:05.313: E/AndroidRuntime(589): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 17:31:05.313: E/AndroidRuntime(589): at java.lang.reflect.Method.invoke(Method.java:511)
05-11 17:31:05.313: E/AndroidRuntime(589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-11 17:31:05.313: E/AndroidRuntime(589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-11 17:31:05.313: E/AndroidRuntime(589): at dalvik.system.NativeStart.main(Native Method)
05-11 17:31:05.313: E/AndroidRuntime(589): Caused by: java.lang.reflect.InvocationTargetException
05-11 17:31:05.313: E/AndroidRuntime(589): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 17:31:05.313: E/AndroidRuntime(589): at java.lang.reflect.Method.invoke(Method.java:511)
05-11 17:31:05.313: E/AndroidRuntime(589): at android.view.View$1.onClick(View.java:3034)
05-11 17:31:05.313: E/AndroidRuntime(589): ... 11 more
05-11 17:31:05.313: E/AndroidRuntime(589): Caused by: java.lang.IllegalArgumentException: Invalid destinationAddress
05-11 17:31:05.313: E/AndroidRuntime(589): at android.telephony.SmsManager.sendTextMessage(SmsManager.java:77)
05-11 17:31:05.313: E/AndroidRuntime(589): at net.learn2develop.sms.SMSActivity.sendSMS(SMSActivity.java:34)
05-11 17:31:05.313: E/AndroidRuntime(589): at net.learn2develop.sms.SMSActivity.onClick(SMSActivity.java:28)
05-11 17:31:05.313: E/AndroidRuntime(589): ... 14 more
提前感谢大家