android實現軟件的服務熱線的功能?


一般軟件都有投訴電話和服務電話等功能,實現起來相對比較簡單,主要記載一下,避免以後用的時候找不到。先看效果圖,因爲不能直接傳本地視頻,所有我用gif的軟件錄的手機上面的視頻。

在這裏插入圖片描述
啓示原理差不多就是跳轉到你的手機撥號,代碼如下:

1.AndroidMainfest頁面的權限

權限代碼如下:

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

在這裏插入圖片描述

2.Acitivity頁面

因爲避免運行有出錯提示ToastUtils,如果你不需要可以直接刪掉,如果你不刪的話需要導入依賴包:

implementation 'com.github.mengpeng920223:ToastUtils:v1.0.3'

如果你刪要刪的話:
在這裏插入圖片描述

直接修改phoneNumber即可撥打到你需要打的號碼了。

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;

import com.mengpeng.mphelper.ToastUtils;

public class MainActivity extends AppCompatActivity {
   
    
    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE =0 ;
    private String phoneNumber="10086";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void startCallPhone(View view) {
   
    
        //判斷Android版本是否大於23
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
   
    
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);

            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
   
    
                ActivityCompat.requestPermissions(this, new String[]{
   
    Manifest.permission.CALL_PHONE},
                        0);
                return;
            } else {
   
    
                callPhone(phoneNumber);
            }
        } else {
   
    
            callPhone(phoneNumber);
            // 檢查是否獲得了權限(Android6.0運行時權限)
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
   
    
                // 沒有獲得授權,申請授權
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.CALL_PHONE)) {
   
    
                    // 返回值:
//                          如果app之前請求過該權限,被用戶拒絕, 這個方法就會返回true.
//                          如果用戶之前拒絕權限的時候勾選了對話框中”Don’t ask again”的選項,那麼這個方法會返回false.
//                          如果設備策略禁止應用擁有這條權限, 這個方法也返回false.
                    // 彈窗需要解釋爲何需要該權限,再次請求授權
                    ToastUtils.onWarnShowToast("Warn toast");

                    // 幫跳轉到該應用的設置界面,讓用戶手動授權
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivity(intent);
                } else {
   
    
                    // 不需要解釋爲何需要該權限,直接請求授權
                    ActivityCompat.requestPermissions(this,
                            new String[]{
   
    Manifest.permission.CALL_PHONE},
                            MY_PERMISSIONS_REQUEST_CALL_PHONE);
                }
            } else {
   
    
                // 已經獲得授權,可以打電話
                callPhone(phoneNumber);
            }
        }

    }

    private void callPhone(String phoneNumber) {
   
    

        Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneNumber));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    // 處理權限申請的回調
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
   
    
        switch (requestCode) {
   
    
            case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
   
    
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
   
    
                    // 授權成功,繼續打電話
                    callPhone(phoneNumber);
                } else {
   
    
                    // 授權失敗!
                }
                break;
            }
        }

    }

}

3.xml文件比較簡單,就一個Button點擊事件

Button裏面的onclick方法是響應的Activity裏面的startCallPhone方法。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="136dp"
        android:text="Button"
        android:onClick="startCallPhone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

方法比較簡單,主要記錄一下,有問題的話可以提出來。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章