Use Activity to complete the following functions

Posted by mattl on Tue, 08 Mar 2022 08:37:12 +0100


1) You can obtain different phone numbers from another Activity to call.

2) You can turn on the camera to take pictures and display them in the picture box

3) You can send text messages

4) The interface is shown in the figure

1, Page layout

Upper code

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageButton
            android:id="@+id/btn_call"
            android:layout_width="135dp"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:background="@mipmap/phonecall" />

        <ImageButton
            android:id="@+id/btn_mail"
            android:layout_width="135dp"
            android:layout_height="100dp"
            android:layout_centerInParent="true"
            android:background="@mipmap/message" />
        <ImageButton
            android:id="@+id/btn_camera"
            android:layout_width="140dp"
            android:layout_height="100dp"
            android:layout_alignParentRight="true"
            android:background="@mipmap/camera"
            />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp">
        <EditText
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:id="@+id/et_phone1"
            android:textSize="20sp"
            android:onClick="onClick"
            android:hint="phoneNum"/>

    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="15dp"
        android:id="@+id/iv_pic"
        />

</LinearLayout>

2, Function realization

Full code demonstration

package com.example.callmessagepicture;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    ImageButton call_btn;
    ImageButton email_btn;
    ImageButton camera_btn;
    EditText et_phoneNum;
    ImageView iv_pic;
    private static int REQUEST_CAMERA = 3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        call_btn = findViewById(R.id.btn_call);
        email_btn = findViewById(R.id.btn_mail);
        camera_btn = findViewById(R.id.btn_camera);
        et_phoneNum = findViewById(R.id.et_phone1);
        iv_pic = findViewById(R.id.iv_pic);
        camera_btn.setOnClickListener(this);
        call_btn.setOnClickListener(this);
        email_btn.setOnClickListener(this);
        if(shouldAskPermissions()){
            askPermissions();
        }
    }
    @Override
    public void onClick(View v){

        if (v.getId()==R.id.et_phone1){
            Intent intent = new Intent();
            intent.setClass(MainActivity.this,ResultActivity.class);
            startActivityForResult(intent,0);
        }else if (v.getId()==R.id.btn_call){
            Intent intent2 = new Intent();
            intent2.setAction(Intent.ACTION_CALL);
            intent2.addCategory(Intent.CATEGORY_DEFAULT);
            String num = et_phoneNum.getText().toString();
            intent2.setData(Uri.parse("tel:"+num));
            startActivityForResult(intent2,1)   ;
        }else if (v.getId()==R.id.btn_camera){
            Intent intent1 = new Intent();
            intent1.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent1,REQUEST_CAMERA);
        }else if (v.getId()==R.id.btn_mail){
            String num = et_phoneNum.getText().toString();
            Uri uri = Uri.parse("smsto:"+num);
            Intent intent3 = new Intent(Intent.ACTION_SENDTO,uri);
            startActivity(intent3);
        }
    }
    @Override
    protected  void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(data == null) return;
        Bundle bundle = data.getExtras();
        if (requestCode==0){
            String phoneNum = (String) bundle.get("phoneNum");
            et_phoneNum.setText(phoneNum);
        } else if (requestCode == 1) {
            String phoneNum=bundle.getString("phoneNum");
            et_phoneNum.setText(phoneNum);
        }else if (requestCode==REQUEST_CAMERA){
            Bitmap b = (Bitmap) bundle.get("data");
            iv_pic.setImageBitmap(b);

        }
    }
    protected boolean shouldAskPermissions(){
        return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
    }
    protected void askPermissions() {
        String[] permissions = {
                "android.permission.CALL_PHONE"
        };
        int requestCode = 200;
        requestPermissions(permissions, requestCode);
    }
}

(1) . obtain different numbers from another Activity and call

From the above layout code, I added the onClick property to the control of EidtText, so that the text edit box can click and jump

The main code implementation of this part

if (v.getId()==R.id.et_phone1){
            Intent intent = new Intent();
            intent.setClass(MainActivity.this,ResultActivity.class);
            startActivityForResult(intent,0);
        }else if (v.getId()==R.id.btn_call){
            Intent intent2 = new Intent();
            intent2.setAction(Intent.ACTION_CALL);
            intent2.addCategory(Intent.CATEGORY_DEFAULT);
            String num = et_phoneNum.getText().toString();
            intent2.setData(Uri.parse("tel:"+num));
            startActivityForResult(intent2,1)   ;
if(data == null) return;
        Bundle bundle = data.getExtras();
        if (requestCode==0){
            String phoneNum = (String) bundle.get("phoneNum");
            et_phoneNum.setText(phoneNum);
        } else if (requestCode == 1) {
            String phoneNum=bundle.getString("phoneNum");
            et_phoneNum.setText(phoneNum);

You also need to add dialing permission - refer to

Android learning - realize simple telephone dialing

Click the text box to jump to the ResultActivity interface

The interface layout code

<?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"
    tools:context=".ResultActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <EditText
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:id="@+id/text_get"/>
        <Button
            android:layout_width="125dp"
            android:layout_height="38dp"
            android:layout_marginLeft="20dp"
            android:background="#F6F4F4"
            android:backgroundTint="#7E7A7A"
            android:text="Select contact"
            app:strokeColor="#6E6B6B"
            android:id="@+id/btn_get"/>
    </LinearLayout>


</LinearLayout>

The intention of this is not to be able to select phone numbers functionally, but to simply see a value transfer method between activities in this way

In the above telephone number implementation code, there is an object bundle to transfer the values between two activities, which corresponds to each other through the same request code and return code

Implementation code in ResultActivity

package com.example.callmessagepicture;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ResultActivity extends AppCompatActivity {
    Button btn_get;
    EditText get_num;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        btn_get = findViewById(R.id.btn_get);
        get_num = findViewById(R.id.text_get);
        btn_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                String phoneNum = get_num.getText().toString();
                intent.putExtra("phoneNum",phoneNum);
                setResult(0,intent);
                finish();
            }
        });
    }ava
}

The value is transferred to MainActivity through putExtra mode of intention

Click the text box to jump to another Activity, and then pass the value back for dialing operation

(2) Get the number from another Activity and send SMS

Implementation code

}else if (v.getId()==R.id.btn_mail){
            String num = et_phoneNum.getText().toString();
            Uri uri = Uri.parse("smsto:"+num);
            Intent intent3 = new Intent(Intent.ACTION_SENDTO,uri);
            startActivity(intent3);
        }

In the same way as making a call, in ET_ The data in phoneNum, that is, the data in the text box, is obtained into the target of SMS to be sent for SMS sending

There's nothing else to say. If you want to refer to authority, you can refer to it

Android learning - add SMS sending, camera calling and map opening functions on the basis of telephone dialing

(3) Start the camera and display the photos taken

else if (v.getId()==R.id.btn_camera){
            Intent intent1 = new Intent();
            intent1.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent1,REQUEST_CAMERA);

Open the camera intentionally and request the returned data

else if (requestCode==REQUEST_CAMERA){
            Bitmap b = (Bitmap) bundle.get("data");
            iv_pic.setImageBitmap(b);

Defines a Bitmap object, converts the returned data into Bitmap format, and puts it into ImageView for display

(4) Effect display

Topics: Android Design Pattern app