在 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拨打电话和发送短信内容请搜索Devmax以前的文章或继续浏览下面的相关文章希望大家以后多多支持Devmax!

Andriod Studio实现拨打电话和发送短信的示例代码的更多相关文章

  1. 如何在Android Studio 1.0.0中更改logcat字体大小?

    我只找到了改变AndroidStudio中字体颜色的方法.解决方法Logcat只使用ConsoleFont的字体设置.要在AndroidStudio中更改此设置,请转到:Settings->Editor->Color&Fonts->ConsoleFont

  2. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  3. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  4. 详解Android Studio实现用户登陆界面demo(xml实现)

    这篇文章主要介绍了详解Android Studio实现用户登陆界面demo,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  5. Android Studio3.6.3 当前最新版本数据库查找与导出方法(图文详解)

    这篇文章主要介绍了Android Studio3.6.3 当前最新版本数据库查找与导出方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android studio实现滑动开关

    这篇文章主要为大家详细介绍了Android studio实现滑动开关,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  7. Android Studio 中运行 groovy 程序的方法图文详解

    这篇文章主要介绍了Android Studio 中 运行 groovy 程序的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  8. Android Studio 4.1没有GsonFormat插件的解决

    这篇文章主要介绍了Android Studio 4.1没有GsonFormat插件的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  9. 如何将Android Studio卸载干净

    这篇文章主要介绍了如何将Android Studio卸载干净,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下

  10. 使用Android Studio创建OpenCV4.1.0 项目的步骤

    这篇文章主要介绍了使用Android Studio创建OpenCV4.1.0 项目的步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部