温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Andriod Studio怎么实现拨打电话和发送短信

发布时间:2022-03-22 13:36:16 来源:亿速云 阅读:427 作者:iii 栏目:开发技术

本篇内容主要讲解“Andriod Studio怎么实现拨打电话和发送短信”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Andriod Studio怎么实现拨打电话和发送短信”吧!

在 Android Studio中创建项目,然后在该项目中创建一个Module名称为“IntentDial”。在该 Module中实现本实例,具体步骤如下:
(1)在新建 Module的res\layout目录下添加布局
文件shouji.xml,将添加的布局管理器设置为相对布局管理器,然后在布局管理器中添加4个用于显示公司信息的文本框,再添加两个 ImageButton 组件,分别为拨打电话按钮和发送短信按钮。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="技术支持:吉林省明日科技有限公司"
        android:layout_marginTop="20dp"/>
        android:id="@+id/text2"
        android:text="网址:http://www.mingrisoft.com"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/text1"/>
        android:id="@+id/text3"
        android:text="企业邮箱:mingrisoft@mingrisoft.com"
        android:layout_below="@+id/text2"/>
        android:id="@+id/text4"
        android:text="技术服务热线:0431-84978981"
        android:layout_below="@+id/text3"/>
    <ImageButton
        android:id="@+id/imageButton_phone"
        android:src="@drawable/phone"
        android:layout_below="@+id/text4"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:background="#0000"
        android:scaleType="fitXY"
        />
        android:id="@+id/imageButton_sms"
        android:layout_toRightOf="@+id/imageButton_phone"
        android:src="@drawable/sms"/>
</RelativeLayout>

(2)修改MainActivity.java文件,在 onCreate(方
法中获取布局文件中的电话图片按钮和短信图
片按钮,并为它们设置单击事件监听器,代码如下:

package com.mingrisoft.intentdial;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shouji);
        //获取电话图片按钮
        ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton_phone);
        //获取短信图片按钮
        ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton_sms);
        imageButton.setOnClickListener(listener); //为电话图片按钮设置单击事件
        imageButton1.setOnClickListener(listener);//为短信图片按钮设置单击事件
    }
  }

(3)在上面的代码中用到了 listener对象,该对象为OnClickListener类型。因此,要在Activity中创建该对象,并重写其 onClick()方法,在该方法中,通过判断单击按钮的id,分别为两个ImageButton组件设置拨打电话和发送短信的 Action及Date,代码如下:

package com.mingrisoft.intentdial;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shouji);
        //获取电话图片按钮
        ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton_phone);
        //获取短信图片按钮
        ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton_sms);
        imageButton.setOnClickListener(listener); //为电话图片按钮设置单击事件
        imageButton1.setOnClickListener(listener);//为短信图片按钮设置单击事件
    }
    //创建监听事件对象
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(); //创建Intent对象
            switch (v.getId()) {       //根据ImageButton组件的id进行判断
                case R.id.imageButton_phone:              //如果是电话图片按钮
                    intent.setAction(intent.ACTION_DIAL); //调用拨号面板
                    intent.setData(Uri.parse("tel:043184978981")); //设置要拨打的号码
                    startActivity(intent); //启动Activity
                    break;
                case R.id.imageButton_sms:             //如果是短信图片按钮
                    intent.setAction(intent.ACTION_SENDTO); //调用发送短信息
                    intent.setData(Uri.parse("smsto:5554")); //设置要发送的号码
                    intent.putExtra("sms_body", "Welcome to Android!"); //设置要发送的信息内容
            }
        }
    };
}

(4)在AndroidManifest.xml文件中,设置允许该应用拨打电话和发送短信的权限,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mingrisoft.intentdial">
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="关于明日学院" android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

运行结果截图:

Andriod Studio怎么实现拨打电话和发送短信

Andriod Studio怎么实现拨打电话和发送短信

Andriod Studio怎么实现拨打电话和发送短信

到此,相信大家对“Andriod Studio怎么实现拨打电话和发送短信”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI