欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Andriod?Studio實(shí)現(xiàn)撥打電話和發(fā)送短信的示例代碼

 更新時(shí)間:2022年03月22日 11:29:34   作者:*年夕  
這篇文章主要介紹了Andriod?Studio實(shí)現(xiàn)撥打電話和發(fā)送短信功能,Android?Studio中創(chuàng)建項(xiàng)目,然后在該項(xiàng)目中創(chuàng)建一個(gè)Module名稱為“IntentDial”,文章結(jié)合實(shí)例步驟給大家介紹的非常詳細(xì),需要的朋友參考下吧

在 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)聽器,代碼如下:

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()方法,在該方法中,通過判斷單擊按鈕的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)聽事件對(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)文章

最新評(píng)論