Android怎么實(shí)現(xiàn)簡(jiǎn)單的撥號(hào)器功能-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)Android怎么實(shí)現(xiàn)簡(jiǎn)單的撥號(hào)器功能的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

我們提供的服務(wù)有:做網(wǎng)站、網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、貢山ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的貢山網(wǎng)站制作公司

簡(jiǎn)易撥號(hào)器的制作方法,具體如下

一、布局構(gòu)造

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="請(qǐng)輸入電話號(hào)碼:"
    android:textSize="30sp"
    />

  <EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView" />

  <Button
    android:id="@+id/dial"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText"
    android:text="Dial"
    android:layout_alignParentRight="true"
    android:layout_marginTop="20dp"
    android:textSize="20sp"/>
</RelativeLayout>

構(gòu)造出布局如圖

Android怎么實(shí)現(xiàn)簡(jiǎn)單的撥號(hào)器功能

二、授予軟件打電話權(quán)限

在AndroidManifest.xml添加如下代碼

<uses-permission android:name="android.permission.CALL_PHONE"/>

授予軟件打電話權(quán)限,否則打不了電話

三、寫代碼(適用于安卓6.0以下)

1).定義一個(gè)外部類去實(shí)現(xiàn)setOnClickListener所需要的接口類型

package com.example.kim.phonedial;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
  private EditText et;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //1.加載布局
    setContentView(R.layout.activity_main);
    //3.找到控件 editText 和 Button
    et=(EditText)findViewById(R.id.editText);
    Button btn=(Button)findViewById(R.id.dial);
    //3.設(shè)置Buuton點(diǎn)擊事件
     btn.setOnClickListener(new MyClickListener());
  }

  //4.定義一個(gè)類去實(shí)現(xiàn)setOnClickListener所需要的接口類型
  private class MyClickListener implements View.OnClickListener{
    public void onClick(View v){
      //5.獲取 editText的文本內(nèi)容
      String num=et.getText().toString().trim();
      if("".equals(num)){
        //Lenth_long 在源代碼中的值為1,Length_short在源代碼中的值為0
        //所以Length_long可直接寫成1,Length_short可直接寫成0
        Toast.makeText(MainActivity.this,"所輸號(hào)碼不能為空",Toast.LENGTH_LONG).show();
        return;
      }
      //6.進(jìn)行撥打電話 意圖Intent
      Intent intent=new Intent();//創(chuàng)建一個(gè)意圖
      //6.1設(shè)置動(dòng)作 打XX
      intent.setAction(Intent.ACTION_CALL);//設(shè)置打的動(dòng)作
      //6.2設(shè)置要撥打的數(shù)據(jù)  uri類型
      // uri統(tǒng)一資源標(biāo)識(shí)符 url統(tǒng)一資源定位符
      intent.setData(Uri.parse("tel:"+num));
      //6.3開啟意圖
      startActivity(intent);
    }
  }
}

2)匿名內(nèi)部類實(shí)現(xiàn)setOnClickListener所需要的接口類型

package com.example.kim.phonedial;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
  private EditText et;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //1.加載布局
    setContentView(R.layout.activity_main);
    //3.找到控件 editText 和 Button
    et=(EditText)findViewById(R.id.editText);
    Button btn=(Button)findViewById(R.id.dial);
    //3.設(shè)置Buuton點(diǎn)擊事件
    // btn.setOnClickListener(new MyClickListener(){});
    btn.setOnClickListener(new View.OnClickListener(){
      public void onClick(View v){
        String num=et.getText().toString().trim();
        if(num.equals("")){
          Toast.makeText(MainActivity.this,"所輸入號(hào)碼不能為空",Toast.LENGTH_LONG).show();
        }else{
          Intent intent=new Intent();
          intent.setAction(Intent.ACTION_CALL);
          intent.setData(Uri.parse("tel:"+num));
          startActivity(intent);
        }
      }
    });
  }
}

四、寫代碼(適用于安卓6.0及以上)

在Android6.0及以上平臺(tái),即便已經(jīng)添加了打電話的權(quán)限,運(yùn)行時(shí)依然會(huì)報(bào)錯(cuò)安全異常:權(quán)限被拒絕。 應(yīng)該在應(yīng)用啟動(dòng)時(shí)檢查應(yīng)用是否被授予電話權(quán)限。

package com.example.kim.phonedial;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
  private EditText et;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //1.加載布局
    setContentView(R.layout.activity_main);
    //3.找到控件 editText 和 Button
    et = (EditText) findViewById(R.id.editText);
    Button btn = (Button) findViewById(R.id.dial);
    //3.設(shè)置Buuton點(diǎn)擊事件
    // btn.setOnClickListener(new MyClickListener(){});
    btn.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        //檢查是否獲得打電話權(quán)限
        //如果沒有獲得電話權(quán)限
        if(ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED){
              if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                  Manifest.permission.CALL_PHONE)){
                  //返回值:
                  //app請(qǐng)求過(guò)該權(quán)限,被用戶拒絕,返回true
                  //用戶拒絕權(quán)限,并勾選了don't ask again,返回false
                  //設(shè)備策略禁止擁有該權(quán)限,返回false

                  //提示用戶授權(quán)
                  Toast.makeText(MainActivity.this,"請(qǐng)授予該應(yīng)用電話權(quán)限",Toast.LENGTH_LONG).show();
                  //跳轉(zhuǎn)到該應(yīng)用設(shè)置界面,幫助用戶授權(quán)
                  Intent intent=new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                  Uri uri=Uri.fromParts("package",getPackageName(),null);
                  intent.setData(uri);
                  startActivity(intent);
              }
        }else{
          CallPhone();
        }

      }
    });
  }
  private void CheckPermission(){

  }
  private void CallPhone() {
    String num = et.getText().toString().trim();
    if (num.equals("")) {
      Toast.makeText(MainActivity.this, "所輸入號(hào)碼不能為空", Toast.LENGTH_LONG).show();
    } else {
      Intent intent = new Intent();
      intent.setAction(Intent.ACTION_CALL);
      intent.setData(Uri.parse("tel:" + num));
      startActivity(intent);
    }
  }
}

感謝各位的閱讀!關(guān)于“Android怎么實(shí)現(xiàn)簡(jiǎn)單的撥號(hào)器功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

標(biāo)題名稱:Android怎么實(shí)現(xiàn)簡(jiǎn)單的撥號(hào)器功能-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://bm7419.com/article2/didpoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)品牌網(wǎng)站建設(shè)、網(wǎng)站改版營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化