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è)置為相對布局管理器,然后在布局管理器中添加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(方
法中獲取布局文件中的電話圖片按鈕和短信圖
片按鈕,并為它們設(shè)置單擊事件監(jiān)聽器,代碼如下:
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對象,該對象為OnClickListener類型。因此,要在Activity中創(chuàng)建該對象,并重寫其 onClick()方法,在該方法中,通過判斷單擊按鈕的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)聽事件對象
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(); //創(chuàng)建Intent對象
switch (v.getId()) { //根據(jù)ImageButton組件的id進(jìn)行判斷
case R.id.imageButton_phone: //如果是電話圖片按鈕
intent.setAction(intent.ACTION_DIAL); //調(diào)用撥號面板
intent.setData(Uri.parse("tel:043184978981")); //設(shè)置要撥打的號碼
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ā)送的號碼
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android如何獲取手機(jī)聯(lián)系人的數(shù)據(jù)庫示例代碼
很多人在做手機(jī)聯(lián)系人的apk時(shí)會(huì)遇到,如何獲取手機(jī)聯(lián)系人數(shù)據(jù)庫的問題,本篇文章主要介紹了android如何獲取手機(jī)聯(lián)系人的數(shù)據(jù)庫示例代碼,有興趣的可以了解一下。2017-01-01
Kotlin類與屬性及構(gòu)造函數(shù)的使用詳解
這篇文章主要介紹了Kotlin語言中類與屬性及構(gòu)造函數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09
Android編程實(shí)現(xiàn)支持拖動(dòng)改變位置的圖片中疊加文字功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)支持拖動(dòng)改變位置的圖片中疊加文字功能,可實(shí)現(xiàn)Android圖片與文字動(dòng)態(tài)操作功能,涉及視圖及圖片動(dòng)態(tài)運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
談?wù)凙ndroid開發(fā)之RecyclerView的使用全解
這篇文章主要介紹了談?wù)凙ndroid開發(fā)之RecyclerView的使用全解,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12
Android實(shí)現(xiàn)簡單底部導(dǎo)航欄 Android仿微信滑動(dòng)切換效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單底部導(dǎo)航欄,Android仿微信滑動(dòng)切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
android Retrofit2網(wǎng)絡(luò)請求封裝介紹
大家好,本篇文章主要講的是android Retrofit2網(wǎng)絡(luò)請求封裝介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12

