1.XML layout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android: text = "number:"
/>
android:id="@+id/phoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:id="@+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android: text = "Call Number" /> waihui real-time spreads
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
android:id="@+id/send_sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android: text = "SMS" />
android:id="@+id/send_sms_noApp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android: text = "Direct SMS" />
2.MainActivity
package com.example.android013;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button call;
private Button send_sms;
private Button send_sms_noApp;
private EditText phoneNumber;
private EditText message;
private String Phone; // define the character
private String msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
call=findViewById(R.id.call);
send_sms=findViewById(R.id.send_sms);
send_sms_noApp=findViewById(R.id.send_sms_noApp);
phoneNumber=findViewById(R.id.phoneNumber);
message=findViewById(R.id.message);
call.setOnClickListener(this);
send_sms.setOnClickListener(this);
send_sms_noApp.setOnClickListener(this);
}
@SuppressLint("WrongConstant")
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.call:
//intention
Intent to = new Intent ();
// set the action
li.setAction (Intent.ACTION_DIAL); // call
Who do you call //
Phone=phoneNumber.getText().toString();
li.setData(Uri.parse("tel:"+Phone));
// Start intent
startActivity (li);
break;
case R.id.send_sms:
//intention
Intent li1=new Intent();
// Get the phone
Phone=phoneNumber.getText().toString();
// Get the message
msg=message.getText().toString();
// set the action
li1.setAction (Intent.ACTION_SENDTO); // where to send
li1.setData (Uri.parse ( "smsto:" + Phone)); // send to who
// send the contents of key-value pairs appear
li1.putExtra ( "sms_body", msg); // a message can only accommodate 140 characters determine the length 70 bytes.
// Start intent
starting activity (LI1);
break;
case R.id.send_sms_noApp: // send yourself, do not start the system comes with APP
// SMS manager
SmsManager manager=SmsManager.getDefault();
//intention
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0,new Intent(),0);
manager.sendTextMessage ( "12306", null, "just go home ah", pendingIntent, null);
Toast.makeText (this, "sent successfully", 0) .show ();
break;
}
}
}