我尝试使用短信管理器发送短信。我有足够的权限。我在Stackoverflow中给出了一些答案。这是我的代码。它适用于我的手机但不发送短信。 '显示短信已发送'帮助我。提前谢谢
package com.example.esms;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText textName = (EditText) findViewById(R.id.editText1);
final EditText textPhone = (EditText) findViewById(R.id.editText2);
Button buttonSend = (Button) findViewById(R.id.button1);
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(textPhone.toString(), null, textName.toString(), null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
}
答案 0 :(得分:3)
将textPhone.toString()
更改为textPhone.getText().toString()
,将textName.toString()
更改为textName.getText().toString()
。