Android Studio——完成电话拨号、短信发送、照相机调用以及打开地图功能

一、创建项目

  • 新建工程
    在这里插入图片描述
  • 工程目录结构
    在这里插入图片描述

二、实现代码

(一)activity_main.xml的编写

  • 完整代码
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
    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=".MainActivity">

    <AbsoluteLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_x="0dp"
        android:layout_y="167dp">

    </AbsoluteLayout>

    <ImageButton
        android:id="@+id/Map_Btn"
        android:layout_width="87dp"
        android:layout_height="96dp"
        android:layout_x="205dp"
        android:layout_y="15dp"
        android:background="@mipmap/map" />

    <ImageButton
        android:id="@+id/Camera_Btn"
        android:layout_width="94dp"
        android:layout_height="96dp"
        android:layout_x="305dp"
        android:layout_y="16dp"
        android:background="@mipmap/camera" />

    <ImageButton
        android:id="@+id/Email_Btn"
        android:layout_width="86dp"
        android:layout_height="96dp"
        android:layout_x="102dp"
        android:layout_y="14dp"
        android:background="@mipmap/email" />

    <ImageButton
        android:id="@+id/Phone_Btn"
        android:layout_width="85dp"
        android:layout_height="96dp"
        android:layout_x="3dp"
        android:layout_y="10dp"
        android:background="@mipmap/phone" />

</AbsoluteLayout>
  • 布局图
    在这里插入图片描述

(二)activity_email.xml的编写

  • 完整代码
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
   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=".EmailActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="176dp"
    android:layout_y="4dp"
    android:text="短信"
    android:textSize="35dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/emailNum"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_x="1dp"
    android:layout_y="144dp"
    android:hint="输入电话号码"
    android:inputType="number" />

<EditText
    android:id="@+id/email_msg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_x="0dp"
    android:layout_y="206dp"
    android:hint="短信内容" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="430dp"
    android:layout_x="-1dp"
    android:layout_y="300dp">

    <ImageButton
        android:id="@+id/send_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@mipmap/email" />
</RelativeLayout>

</AbsoluteLayout>

  • 布局图
    在这里插入图片描述

(三)MainActivity.java的编写

  • 完整代码
package com.example.app4;


import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageButton;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    ImageButton phone_btn;
    ImageButton email_btn;
    ImageButton camera_btn;
    ImageButton map_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        phone_btn=findViewById(R.id.Phone_Btn);
        email_btn=findViewById(R.id.Email_Btn);
        camera_btn=findViewById(R.id.Camera_Btn);
        map_btn=findViewById(R.id.Map_Btn);
        map_btn.setOnClickListener(this);
        camera_btn.setOnClickListener(this);
        email_btn.setOnClickListener(this);
        phone_btn.setOnClickListener(this);
    }
    @Override
    public void onClick(View v){
        Intent intent=new Intent();
        switch (v.getId()){
            case R.id.Phone_Btn:
                intent.setClassName("com.example.app3","com.example.app3.MainActivity");
                break;
            case R.id.Email_Btn:
                intent.setClass(this,EmailActivity.class);
                break;
            case R.id.Camera_Btn:
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                break;
            case R.id.Map_Btn:
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("geo:39.899533,166.036476"));
                break;
        }
        startActivity(intent);
    }

}

(四)EmailActivity.java的编写

  • 完整代码
package com.example.app4;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;

public class EmailActivity extends AppCompatActivity {
    EditText emailNum;
    EditText email_msg;
    ImageButton send_btn;

    private static final int SEND_SMS=100;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email);
        if(shouldAskPermissions()){
            askPermissions();
        }
        email_msg=findViewById(R.id.email_msg);
        emailNum=findViewById(R.id.emailNum);
        send_btn=findViewById(R.id.send_btn);
        send_btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                requestPermission();
            }
        });
    }
    protected boolean shouldAskPermissions(){
        return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
    }
    protected void askPermissions(){
        String[] permissions = {
                "android.permissions.CALL_PHONE"
        };
        int requestCode=200;
        requestPermissions(permissions,requestCode);
    }
    private void requestPermission(){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1){
            int checkCallphonePermission= ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
            if (checkCallphonePermission != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS},SEND_SMS);
                return;
            }else{
                sendSMSS();
            }
        }else{

        }
    }
    public void onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults){
        switch (requestCode){
            case SEND_SMS:
                if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    sendSMSS();
                }else{
                    Toast.makeText(EmailActivity.this,"CALL_PHONE Denied",Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
    private void sendSMSS(){
        String content=email_msg.getText().toString().trim();
        String phone=emailNum.getText().toString().trim();
        if(!content.isEmpty() && !phone.isEmpty()){
            SmsManager manager=SmsManager.getDefault();
            ArrayList<String> strings=manager.divideMessage(content);
            for (int i=0;i<strings.size();i++){
                manager.sendTextMessage(phone,null,content,null,null);
            }
            Toast.makeText(EmailActivity.this,"发送成功",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"手机号或内容不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
    }
}

三、实现效果

  • 主界面
    在这里插入图片描述

  • 电话拨号
    点击电话图标,就会调用app3中的activity
    在这里插入图片描述

  • 短信发送
    在这里插入图片描述

  • 打开地图
    在这里插入图片描述
    选择高德地图,就会打开高德地图
    在这里插入图片描述

  • 选择照相机功能时,会打开系统相机
    在这里插入图片描述

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android Studio调用相机需要以下几个步骤: 1. 在 AndroidManifest.xml 文件中添加相机的权限: ```xml <uses-permission android:name="android.permission.CAMERA" /> ``` 2. 在布局文件中添加一个用于预览相机的 SurfaceView 或 TextureView: ```xml <SurfaceView android:id="@+id/surfaceView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 3. 在相应的 Activity 或 Fragment 中获取 Camera 实例,并设置预览显示: ```java private Camera mCamera; private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSurfaceView = findViewById(R.id.surfaceView); mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); } @Override public void surfaceCreated(SurfaceHolder holder) { // 打开相机并设置预览显示 mCamera = Camera.open(); try { mCamera.setPreviewDisplay(holder); mCamera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // 预览尺寸发生变化时处理 } @Override public void surfaceDestroyed(SurfaceHolder holder) { // 关闭相机释放资源 if (mCamera != null) { mCamera.stopPreview(); mCamera.release(); mCamera = null; } } ``` 4. 添加拍照功能,通过调用 `Camera.takePicture()` 方法实现: ```java private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { // 处理拍照数据 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); // 保存或展示拍照结果 } }; public void takePicture(View view) { if (mCamera != null) { mCamera.takePicture(null, null, mPictureCallback); } } ``` 以上是一个简单的调用相机并拍照的示例,你可以根据需要进行更加详细的定制和功能扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值