修改以下代码public class MainActivity extends AppCompatActivity { private EditText number,content; private Button send; SmsManager smsManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取SmsManager smsManager = SmsManager.getDefault(); number = (EditText) findViewById(R.id.number); content = (EditText) findViewById(R.id.content); send = (Button) findViewById(R.id.send); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,new Intent(),0); Log.i("Main","Pengding"+ pendingIntent); String phone = number.getText().toString(); String msg = content.getText().toString(); smsManager.sendTextMessage(phone,null,msg,pendingIntent,null); Log.i("Main","smsmanager"); Toast.makeText(MainActivity.this,"发送成功",Toast.LENGTH_SHORT); } }); } }
时间: 2024-02-14 18:13:06 浏览: 85
Android使用SmsManager实现短信发送功能
您需要在Toast.makeText()方法中添加.show()才能够显示Toast提示信息,修改后的代码如下:
```
public class MainActivity extends AppCompatActivity {
private EditText number,content;
private Button send;
SmsManager smsManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取SmsManager
smsManager = SmsManager.getDefault();
number = (EditText) findViewById(R.id.number);
content = (EditText) findViewById(R.id.content);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,new Intent(),0);
Log.i("Main","Pengding"+ pendingIntent);
String phone = number.getText().toString();
String msg = content.getText().toString();
smsManager.sendTextMessage(phone,null,msg,pendingIntent,null);
Log.i("Main","smsmanager");
Toast.makeText(MainActivity.this,"发送成功",Toast.LENGTH_SHORT).show();
}
});
}
}
```
这样就可以在发送短信成功后弹出Toast提示“发送成功”了。
阅读全文