Android --Intent basis of Action and Data property

Layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打电话"
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发短信"
        />

</LinearLayout>

java call

package com.example.myintentii;

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 MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1 =(The Button) the findViewById (R.id.button1); 
        the Button Button2 = (the Button) the findViewById (R.id.button2);
         // Set the listener object 
        button1.setOnClickListener (listener); 
        button2.setOnClickListener (listener); 
    } 

    View. listener OnClickListener to = new new View.OnClickListener () { 
        @Override // point button listener 
        public  void the onClick (View V) { 
            the Intent Intent = new new the Intent (); 
            the button button = (the button) V;
             Switch (button.getId ()) {
                 CaseR.id.button1:   // call 
                    intent.setAction (intent.ACTION_DIAL); // set called the ACTION 
                    intent.setData (Uri.parse ( "tel: 00000000")); // Set the telephone number 
                    startActivity (intent) ;
                     BREAK ;
                 Case R.id.button2:   // texting 
                    intent.setAction (intent.ACTION_SENDTO); 
                    intent.setData (Uri.parse ( "smsto: 11111111" )); 
                    intent.putExtra ( "SMS_BODY", "the Hello world "); // edit message content 
                    startActivity (intent);
                    break;
            }
        }
    };
}

 

Guess you like

Origin www.cnblogs.com/zsben991126/p/12236963.html