Andriod?Studio實現(xiàn)撥打電話和發(fā)送短信的示例代碼
在 Android Studio中創(chuàng)建項目,然后在該項目中創(chuàng)建一個Module名稱為“IntentDial”。在該 Module中實現(xiàn)本實例,具體步驟如下:
(1)在新建 Module的res\layout目錄下添加布局
文件shouji.xml,將添加的布局管理器設(shè)置為相對布局管理器,然后在布局管理器中添加4個用于顯示公司信息的文本框,再添加兩個 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,分別為兩個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進行判斷 case R.id.imageButton_phone: //如果是電話圖片按鈕 intent.setAction(intent.ACTION_DIAL); //調(diào)用撥號面板 intent.setData(Uri.parse("tel:043184978981")); //設(shè)置要撥打的號碼 startActivity(intent); //啟動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>
運行結(jié)果截圖:
到此這篇關(guān)于Andriod Studio實現(xiàn)撥打電話和發(fā)送短信功能的文章就介紹到這了,更多相關(guān)Andriod Studio撥打電話和發(fā)送短信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
dialog dismiss時鍵盤不消失的問題淺析及解決辦法
這篇文章主要介紹了dialog dismiss時鍵盤不消失的問題淺析及兩種解決方法,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01Android使用ContentProvider實現(xiàn)查看系統(tǒng)短信功能
這篇文章主要為大家詳細介紹了Android使用ContentProvider實現(xiàn)查看系統(tǒng)短信功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11用Android Studio3.0新功能加快構(gòu)建速度
本文主要介紹了使用Android Studio3.0新功能,加快Android Studio的構(gòu)建速度等相關(guān)做法。2017-11-11Android自定義View 實現(xiàn)水波紋動畫引導(dǎo)效果
在android程序開發(fā)中,我們經(jīng)常簡單通過自定義view實現(xiàn)水波紋動畫引導(dǎo)功能,下面通過本文給大家分享實現(xiàn)代碼,需要的朋友參考下2017-01-01Android開發(fā) 旋轉(zhuǎn)屏幕導(dǎo)致Activity重建解決方法
Android開發(fā)文檔上專門有一小節(jié)解釋這個問題。簡單來說,Activity是負責(zé)與用戶交互的最主要機制,接下來為您詳細介紹2012-11-11Android中使用itemdecoration實現(xiàn)時間線效果
這篇文章主要介紹了Android中使用itemdecoration實現(xiàn)時間線效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02Android編程設(shè)計模式之Builder模式實例詳解
這篇文章主要介紹了Android編程設(shè)計模式之Builder模式,結(jié)合實例形式詳細分析了Android設(shè)計模式之Builder模式概念、功能、使用場景、用法及相關(guān)注意事項,需要的朋友可以參考下2017-12-12Android studio 4.1打包失敗和插件錯誤提示的解決
這篇文章主要介紹了Android studio 4.1打包失敗和插件錯誤提示的解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10