Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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?

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report