Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Android use the Action and Data properties of Intent to click the button to jump to the interface for making phone calls and sending text messages?

Shulou Source: shulou.com Published: 2022-06-03 09:38:17 09月29日 Update

Editor to share with you how Android uses the Action and Data properties of Intent to click the button to jump to the interface of making phone calls and sending text messages. I hope you will get something after reading this article. Let's discuss it together.

Realize

Change the layout to LinearLayout, set it to vertical through android:orientation= "vertical" >, and then add the id attribute.

Then add two buttons and set the Id property and display text.

Then come to Activity. First, get two Button through ID.

Button buttonCall = (Button) findViewById (R.id.call); Button buttonSend = (Button) findViewById (R.id.send)

And because the two Button click event listeners are similar, all pull out a common click event listener object.

View.OnClickListener listener = new View.OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (); / / convert view to Button Button button = (Button) v; / / according to button's id switch (button.getId ()) {/ / if you dial the phone button case R.id.call: / / set Action behavior property intent.setAction (intent.ACTION_DIAL) / / 123456789 after setting data is the default phone to be dialed: intent.setData (Uri.parse ("tel:123456789")); startActivity (intent); break; case R.id.send: / / set the behavior to send SMS intent.setAction (intent.ACTION_SENDTO); / / set to send to 10086 intent.setData (Uri.parse ("smsto:10086")) / / set the default sending content of SMS intent.putExtra ("sms_body", "official account: overbearing programmer"); startActivity (intent); break;}

Then set a click event listener on the button in OnCreate.

Complete sample code

Package com.badao.relativelayouttest;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Button;public class IntentActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_intent); Button buttonCall = (Button) findViewById (R.id.call); Button buttonSend = (Button) findViewById (R.id.send); buttonCall.setOnClickListener (listener); buttonSend.setOnClickListener (listener) } View.OnClickListener listener = new View.OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (); / / convert view to Button Button button = (Button) v; / / according to button's id switch (button.getId ()) {/ / if you dial the phone button case R.id.call: / / set Action behavior property intent.setAction (intent.ACTION_DIAL) / / 123456789 after setting data is the default phone to be dialed: intent.setData (Uri.parse ("tel:123456789")); startActivity (intent); break; case R.id.send: / / set the behavior to send SMS intent.setAction (intent.ACTION_SENDTO); / / set to send to 10086 intent.setData (Uri.parse ("smsto:10086")) / / set the default SMS content intent.putExtra ("sms_body", "official account: overbearing programmer"); startActivity (intent); break;};}

Because phone calls and text messages are used, you need to declare these two permissions and open AndroidMainfest.xml.

After reading this article, I believe you have a certain understanding of "how Android uses the Action and Data properties of Intent to click the button to jump to the interface for making phone calls and sending text messages". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Tags: Text message attribute button phone two behavior event listener monitor interface overbearing public content layout data program article almost code done Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple macOS Linux Redmi