我发送简单的功能来发送短信。
public void send(String address, String packet){//address="13614951242" packet="a"
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(address, null, packet, null, null);
}
但是,有NullPointErexception Bug。
10-12 03:20:05.463: ERROR/AndroidRuntime(9662): Caused by: java.lang.NullPointerException
10-12 03:20:05.463: ERROR/AndroidRuntime(9662): at android.os.Parcel.readException(Parcel.java:1253)
10-12 03:20:05.463: ERROR/AndroidRuntime(9662): at android.os.Parcel.readException(Parcel.java:1235)
10-12 03:20:05.463: ERROR/AndroidRuntime(9662): at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:561)
10-12 03:20:05.463: ERROR/AndroidRuntime(9662): at android.telephony.SmsManager.sendTextMessage(SmsManager.java:109)
10-12 03:20:05.463: ERROR/AndroidRuntime(9662): at ric.FPSProject.SinglePos.SinglePosMap.initPersonData(SinglePosMap.java:260)
10-12 03:20:05.463: ERROR/AndroidRuntime(9662): at ric.FPSProject.SinglePos.SinglePosMap.onResume(SinglePosMap.java:160)
10-12 03:20:05.463: ERROR/AndroidRuntime(9662): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1151)
10-12 03:20:05.463: ERROR/AndroidRuntime(9662): at android.app.Activity.performResume(Activity.java:3823)
10-12 03:20:05.463: ERROR/AndroidRuntime(9662): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3123)
10-12 03:20:05.463: ERROR/AndroidRuntime(9662): ... 12 more
怎么了?救救我!
答案 0 :(得分:0)
您是否尝试让应用程序发送短信,或者您是否尝试打开默认短信应用程序来发送短信?如果你想要做的更晚,你需要做的就是:
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms:"));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("sms_body", "your string here");
startActivity(i);
这将打开默认短信,并在消息正文中显示“your string here”字符串。
答案 1 :(得分:0)
嗯,为我工作,所以我猜你在其他地方有一个bug。这就是我试过的:
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
public class Playground extends Activity {
Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
send("13614951242", "a");
}
});
}
private void send(String number, String message) {
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(number, null, message, null, null);
}
}
答案 2 :(得分:0)
您的代码是正确的,因此其他地方一定存在问题。确保您在Android Manifest中拥有以下权限:
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>