Andriod?Studio實(shí)現(xiàn)撥打電話和發(fā)送短信的示例代碼
在 Android Studio中創(chuàng)建項(xiàng)目,然后在該項(xiàng)目中創(chuàng)建一個(gè)Module名稱為“IntentDial”。在該 Module中實(shí)現(xiàn)本實(shí)例,具體步驟如下:
(1)在新建 Module的res\layout目錄下添加布局
文件shouji.xml,將添加的布局管理器設(shè)置為相對(duì)布局管理器,然后在布局管理器中添加4個(gè)用于顯示公司信息的文本框,再添加兩個(gè) ImageButton 組件,分別為撥打電話按鈕和發(fā)送短信按鈕。代碼如下:
<?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="技術(shù)支持:吉林省明日科技有限公司"
android:layout_marginTop="20dp"/>
android:id="@+id/text2"
android:text="網(wǎng)址:http://www.mingrisoft.com"
android:layout_marginTop="10dp"
android:layout_below="@+id/text1"/>
android:id="@+id/text3"
android:text="企業(yè)郵箱:mingrisoft@mingrisoft.com"
android:layout_below="@+id/text2"/>
android:id="@+id/text4"
android:text="技術(shù)服務(wù)熱線: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(方
法中獲取布局文件中的電話圖片按鈕和短信圖
片按鈕,并為它們?cè)O(shè)置單擊事件監(jiān)聽(tīng)器,代碼如下:
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); //為電話圖片按鈕設(shè)置單擊事件
imageButton1.setOnClickListener(listener);//為短信圖片按鈕設(shè)置單擊事件
}
}(3)在上面的代碼中用到了 listener對(duì)象,該對(duì)象為OnClickListener類型。因此,要在Activity中創(chuàng)建該對(duì)象,并重寫其 onClick()方法,在該方法中,通過(guò)判斷單擊按鈕的id,分別為兩個(gè)ImageButton組件設(shè)置撥打電話和發(fā)送短信的 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); //為電話圖片按鈕設(shè)置單擊事件
imageButton1.setOnClickListener(listener);//為短信圖片按鈕設(shè)置單擊事件
}
//創(chuàng)建監(jiān)聽(tīng)事件對(duì)象
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(); //創(chuàng)建Intent對(duì)象
switch (v.getId()) { //根據(jù)ImageButton組件的id進(jìn)行判斷
case R.id.imageButton_phone: //如果是電話圖片按鈕
intent.setAction(intent.ACTION_DIAL); //調(diào)用撥號(hào)面板
intent.setData(Uri.parse("tel:043184978981")); //設(shè)置要撥打的號(hào)碼
startActivity(intent); //啟動(dòng)Activity
break;
case R.id.imageButton_sms: //如果是短信圖片按鈕
intent.setAction(intent.ACTION_SENDTO); //調(diào)用發(fā)送短信息
intent.setData(Uri.parse("smsto:5554")); //設(shè)置要發(fā)送的號(hào)碼
intent.putExtra("sms_body", "Welcome to Android!"); //設(shè)置要發(fā)送的信息內(nèi)容
}
}
};
}(4)在AndroidManifest.xml文件中,設(shè)置允許該應(yīng)用撥打電話和發(fā)送短信的權(quán)限,代碼如下:
<?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="關(guān)于明日學(xué)院" 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>運(yùn)行結(jié)果截圖:



到此這篇關(guān)于Andriod Studio實(shí)現(xiàn)撥打電話和發(fā)送短信功能的文章就介紹到這了,更多相關(guān)Andriod Studio撥打電話和發(fā)送短信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
dialog dismiss時(shí)鍵盤不消失的問(wèn)題淺析及解決辦法
這篇文章主要介紹了dialog dismiss時(shí)鍵盤不消失的問(wèn)題淺析及兩種解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
Android使用ContentProvider實(shí)現(xiàn)查看系統(tǒng)短信功能
這篇文章主要為大家詳細(xì)介紹了Android使用ContentProvider實(shí)現(xiàn)查看系統(tǒng)短信功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
用Android Studio3.0新功能加快構(gòu)建速度
本文主要介紹了使用Android Studio3.0新功能,加快Android Studio的構(gòu)建速度等相關(guān)做法。2017-11-11
Android自定義View 實(shí)現(xiàn)水波紋動(dòng)畫引導(dǎo)效果
在android程序開(kāi)發(fā)中,我們經(jīng)常簡(jiǎn)單通過(guò)自定義view實(shí)現(xiàn)水波紋動(dòng)畫引導(dǎo)功能,下面通過(guò)本文給大家分享實(shí)現(xiàn)代碼,需要的朋友參考下2017-01-01
Android開(kāi)發(fā) 旋轉(zhuǎn)屏幕導(dǎo)致Activity重建解決方法
Android開(kāi)發(fā)文檔上專門有一小節(jié)解釋這個(gè)問(wèn)題。簡(jiǎn)單來(lái)說(shuō),Activity是負(fù)責(zé)與用戶交互的最主要機(jī)制,接下來(lái)為您詳細(xì)介紹2012-11-11
Android中使用itemdecoration實(shí)現(xiàn)時(shí)間線效果
這篇文章主要介紹了Android中使用itemdecoration實(shí)現(xiàn)時(shí)間線效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Android編程設(shè)計(jì)模式之Builder模式實(shí)例詳解
這篇文章主要介紹了Android編程設(shè)計(jì)模式之Builder模式,結(jié)合實(shí)例形式詳細(xì)分析了Android設(shè)計(jì)模式之Builder模式概念、功能、使用場(chǎng)景、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-12-12
Android activity和view判斷滑動(dòng)
這篇文章主要介紹了Android activity和view判斷滑動(dòng)的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android studio 4.1打包失敗和插件錯(cuò)誤提示的解決
這篇文章主要介紹了Android studio 4.1打包失敗和插件錯(cuò)誤提示的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

