androidstudio能实现简单的电话拨号,短信发送,照相机调用,地图打开功能

一、电话拨打

(一)代码设计

1.PhoneActivity

package com.example.helloworld1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import com.example.helloworld.R;

public class PhoneActivity extends AppCompatActivity {
    EditText phoneNum;
    ImageButton call_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);
        phoneNum=(EditText) findViewById(R.id.phoneNum);
        call_btn=(ImageButton) findViewById(R.id.call_btn);
        if(shouldAskPermissions()){
            askPermissions();
        }
        call_btn = findViewById(R.id.call_btn);
        call_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.addCategory(intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("tel:" + phoneNum.getText()));
                startActivity(intent);

            }

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


在这里插入图片描述

2.activity_phone.xml

<?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:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"
    android:orientation="vertical"
    tools:context="com.example.helloworld1.PhoneActivity">

    <TextView
        android:layout_width="325dp"
        android:layout_height="30dp"
        android:text="电话拨号"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入电话号码"
        android:inputType="number"
        android:id="@+id/phoneNum"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageButton
            android:id="@+id/call_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@mipmap/img" />

    </RelativeLayout>
</LinearLayout>

在这里插入图片描述
3.权限设置
AndroidManifest.xml

   <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />

(二)页面演示

选择页面
在这里插入图片描述
进入拨号页面
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

二、 短信发送

(一)代码设计

1.MailAcitivity

package com.example.helloworld1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import com.example.helloworld.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import java.util.List;


public class MailActivity extends AppCompatActivity {
  


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mail);
        EditText et_name=(EditText)findViewById(R.id.num_btn);
        EditText et_message=(EditText)findViewById(R.id.mes_btn);
        Button  bt_send=(Button)findViewById(R.id.send_btn);
        bt_send.setOnClickListener(new View.OnClickListener(){
        //第二步点击事件
   @Override
        public void onClick(View v) {
       //3.事件处理
       //3.1获取相关信息
       String name = et_name.getText().toString();
       String message = et_message.getText().toString();

       Uri nameUri = Uri.parse("smsto:" + name);
       Intent returnIt = new Intent();
       returnIt.setAction(Intent.ACTION_SENDTO);//发短信的action
       returnIt.setData(nameUri);
       returnIt.putExtra("sms_body",message);
       //1.获取短信管理器
       //3.2发送短信
       startActivity(returnIt);
   }
        });

    }


}

2.activity_mail.xml


<?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"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"
    tools:context="com.example.helloworld1.MailActivity">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="电话号码"
        android:inputType="number"
        android:id="@+id/num_btn"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="短信内容"
        android:id="@+id/mes_btn"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="发送"
        android:textColor="#ff0066"
        android:id="@+id/send_btn"/>

</LinearLayout>

3.权限设置
AndroidManifest.xml

 <uses-permission android:name="android.permission.SEND_SMS" />

(二)页面演示

1.输入电话
在这里插入图片描述
2.输入信息

3.成功传递调用短信
在这里插入图片描述

4.未发送,保存草稿
在这里插入图片描述

三、照相机调用

(一)代码设计

1.CarmeraAcitiviy

ackage com.example.helloworld1;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.helloworld.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CameraActivity extends AppCompatActivity {
    public static final int TAKE_PHOTO = 1;//声明一个请求码,用于识别返回的结果
    private ImageView picture;
    private Uri imageUri;
    private final String filePath = Environment.getExternalStorageDirectory() + File.separator + "output_image.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER);

//重写onActivityResult方法
        //点击事件进行拍照
        Button takephoto = findViewById(R.id.take_photo);
        picture = findViewById(R.id.picture);
        takephoto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                //动态请求相机权限
                requestPermission();  //在其中若用户给予权限则请求相机拍照
            }
        });
        //设置默认图片
        setDefualtImage();
    }

    //动态请求权限
    private void requestPermission() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            //请求权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
        } else {
            //调用
            requestCamera();
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults != null && grantResults.length != 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            switch (requestCode) {
                case 1: {
                    requestCamera();
                }
                break;
            }
        }
    }


    private void requestCamera() {
        File outputImage = new File(filePath);
                /*
                创建一个File文件对象,用于存放摄像头拍下的图片,我们把这个图片命名为output_image.jpg
                并把它存放在应用关联缓存目录下,调用getExternalCacheDir()可以得到这个目录,为什么要
                用关联缓存目录呢?由于android6.0开始,读写sd卡列为了危险权限,使用的时候必须要有权限,
                应用关联目录则可以跳过这一步
                 */
        try//判断图片是否存在,存在则删除在创建,不存在则直接创建
        {
            if (!outputImage.getParentFile().exists()) {
                outputImage.getParentFile().mkdirs();
            }
            if (outputImage.exists()) {
                outputImage.delete();
            }

            outputImage.createNewFile();

            if (Build.VERSION.SDK_INT >= 24) {
                imageUri = FileProvider.getUriForFile(this,
                        "com.example.mydemo.fileprovider", outputImage);
            } else {
                imageUri = Uri.fromFile(outputImage);
            }
            //使用隐示的Intent,系统会找到与它对应的活动,即调用摄像头,并把它存储
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, TAKE_PHOTO);
            //调用会返回结果的开启方式,返回成功的话,则把它显示出来
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //处理返回结果的函数,下面是隐示Intent的返回结果的处理方式,具体见以前我所发的intent讲解
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case TAKE_PHOTO:
                if (resultCode == RESULT_OK) {
                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        picture.setImageBitmap(bitmap);
                        //将图片解析成Bitmap对象,并把它显现出来
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
            default:
                break;
        }
    }

    //设置保存拍照图片——>再次关闭app重新打开显示为上次拍照照片
    private void setDefualtImage() {
        File outputImage = new File(filePath);
        if (!outputImage.exists()) {
            return;
        }
        picture.setImageBitmap(BitmapFactory.decodeFile(filePath));
    }
}

activity_carmera.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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:collapseColumns="0,2"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"
    tools:context="com.example.helloworld1.CameraActivity">

    <Button
        android:id="@+id/take_photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="take photo"
        />
    <ImageView
        android:id="@+id/picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        />

</TableLayout>

(二)页面演示

1.打开相机页面
在这里插入图片描述
2.点击TAKE PHOTO
在这里插入图片描述
3.照片
在这里插入图片描述

四、地图打开

(一)代码设计

1.MapAcivity

public class MapActivity extends AppCompatActivity {
EditText mapnum;
Button map_btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    mapnum = (EditText) findViewById(R.id.mapNum);
    map_btn = (Button) findViewById(R.id.map_btn);
    map_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            Uri uri = Uri.parse(("geo:" + mapnum.getText()));
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(uri);
            startActivity(intent);

        }
    });
}

}
2.activity_map.xml

<?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:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.helloworld1.MapActivity">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入经纬度"
        android:inputType="text"
        android:id="@+id/mapNum"/>
    <Button
        android:text="打开地图"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/map_btn"/>

</LinearLayout>

(二)页面演示

1.点击地图打开页面
在这里插入图片描述
2.输入经纬度,打开地图

3.选择百度地图,打开成功
在这里插入图片描述

  • 5
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我简单介绍一下在Android Studio实现组件通信和发送短信的方法。 1. 组件通信 组件通信可以通过Intent实现,Intent是一种用于在不同组件(Activity、Service等)之间传递消息的机制。以下是一个简单的示例,演示如何通过Intent实现Activity之间的通信。 在发送方的Activity中: ```java // 创建Intent对象 Intent intent = new Intent(this, ReceiverActivity.class); // 将数据放入Intent中 intent.putExtra("data", "Hello, ReceiverActivity!"); // 启动ReceiverActivity,并传递Intent startActivity(intent); ``` 在接收方的Activity中: ```java // 获取传递过来的Intent Intent intent = getIntent(); // 获取Intent中的数据 String data = intent.getStringExtra("data"); // 在界面上显示数据 TextView textView = findViewById(R.id.text_view); textView.setText(data); ``` 以上代码演示了如何在Activity之间传递简单的字符串数据,实际应用中可以传递各种类型的数据,包括自定义对象等。 2. 发送短信Android发送短信可以通过调用系统的短信接口实现,以下是一个简单的示例,演示如何在应用中发送短信。 ```java // 创建SmsManager对象 SmsManager smsManager = SmsManager.getDefault(); // 发送短信 smsManager.sendTextMessage("目标手机号码", null, "短信内容", null, null); ``` 以上代码中,`sendTextMessage`方法的参数依次是目标手机号码、发送方号码(可以为null)、短信内容、发送状态的回执(可以为null)、短信发送成功的回执(可以为null)。实际使用中,可以根据需要自行调整参数。 注意:在Android 6.0及以上版本中,要发送短信需要获取短信发送权限,可以在`AndroidManifest.xml`文件中添加以下代码: ```xml <uses-permission android:name="android.permission.SEND_SMS" /> ``` 以上就是使用Android Studio实现组件通信和发送短信的基本方法,希望能对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值