簡體   English   中英

如果在Android Studio中按下按鈕,則無法撥打電話

[英]Can't dial a phone on the event of a button press in Android Studio

在我的android應用中,我有一個帶有幾個按鈕的導航抽屜,這些按鈕可以打開具有相關布局的不同片段。

該按鈕中的一個應該調用一個功能,該功能理論上應該執行電話撥號。 它似乎不起作用。 什么也沒發生,導航抽屜剛剛關閉。 我已經在清單中實現了此許可權,為了獲得許可權,該清單應該起作用:

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

我的代碼基於導航抽屜中的按鈕事件執行代碼,如下所示:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    android.app.FragmentManager fragmentmanager = getFragmentManager();

    if (id == R.id.nav_forside) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new MainFragment())
                .commit();

    } else if (id == R.id.nav_matif) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new MatifFragment())
                .commit();

    } else if (id == R.id.nav_om) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new OmFragment())
                .commit();

    } else if (id == R.id.nav_rk) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new RkFragment())
                .commit();

    } else if (id == R.id.nav_bd) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new BdFragment())
                .commit();

    } else if (id == R.id.nav_rf) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new RfFragment())
                .commit();

    } else if (id == R.id.nav_kontakt_os) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new KontaktOsFragment())
                .commit();

    } else if (id == R.id.nav_call_number) {
    callNumber();

    }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

最后但並非最不重要的一點是,執行電話呼叫的功能如下所示: 我必須指出,如果我不檢查程序包管理器是否已授予訪問權限以執行電話呼叫,則會引發錯誤。 這就是為什么代碼檢查是否授予權限的原因:

public void callNumber () {
            //Tries to call the phone number, and catches any errors that might be present
            try {
                //Checking if the the permission to call a phone is granted
                if (ActivityCompat.checkSelfPermission(MainActivity.this.getApplicationContext(), Manifest.permission.CALL_PHONE) ==
                        PackageManager.PERMISSION_GRANTED) {

                    Intent i = new Intent(Intent.ACTION_DIAL);
                    String p = "tel:12345678";
                    i.setData(Uri.parse(p));
                    startActivity(i);
                }
            } catch (Exception exLog) {
                //Catches error that prevents phone from calling, and expresses itself in form of a alertdialog
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                alertDialog.setTitle("Opkaldet kunne ikke gennemføres");
                alertDialog.setMessage(exLog.toString());
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                alertDialog.show();
            }
        }

我知道Amiars的答案有效,但這並不是正確的答案。

問題是:您不需要Intent.ACTION_DIAL任何許可。 您需要權限CALL_PHONE才能使用Intent.ACTION_CALL

不同之處在於ACTION_CALL實際上執行了呼叫,這有些危險,因為它通常會給用戶帶來費用,而ACTION_DIAL僅打開撥號程序,並且用戶將通過按下呼叫按鈕來明確完成操作。 全部在文檔中: https : //developer.android.com/reference/android/content/Intent.html

ACTION_CALL

活動行動:向某人打電話(...)

注意:哪些應用程序可以發起呼叫受到限制; 大多數應用程序應使用ACTION_DIAL

注意:如果您的應用定位到M或更高版本,並且聲明使用的CALL_PHONE權限未被授予,則嘗試使用此操作將導致SecurityException。

ACTION_DIAL

活動操作:撥打數據指定的號碼。 這顯示了帶有撥號號碼的用戶界面,允許用戶明確發起呼叫

因此正確的答案是:

if有權限, if刪除,然后直接調用startActivity(i);

編輯:

我只是在最新的AndroidStudio上鍵入了該代碼,而未對清單進行任何許可,它可以正常工作。 無警告,在設備上執行時將打開撥號程序。

 public static void call(Context context, String number) {
    Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number));
    try {
      context.startActivity(i);
    } catch (Exception e) {
      // this can happen if the device can't make phone calls
      // for example, a tablet
    }
  }

我還可以指出其他幾個問相同問題的問題:

如何打開顯示電話號碼的撥號器?

使用Intent撥打電話時需要許可嗎?

如何在Android中使用選定號碼在手機上打開撥號程序

意向呼叫操作不適用於棉花糖

https://stackoverflow.com/a/13123613/906362

希望對您有所幫助。

如果未授予許可怎么辦?

在棉花糖之后的新版本android中,您需要像以前一樣檢查代碼中的權限,但是如果未授予權限,則需要請求權限。 此版本android的cuz清單中的權限將無法正常工作。

if (ActivityCompat.checkSelfPermission(MainActivity.this.getApplicationContext(), Manifest.permission.CALL_PHONE) ==
                    PackageManager.PERMISSION_GRANTED) {

                Intent i = new Intent(Intent.ACTION_DIAL);
                String p = "tel:12345678";
                i.setData(Uri.parse(p));
                startActivity(i);
}
else
{
  ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CALL_PHONE}, 0);
}

您還可以在以下活動中為授予的案例權限設置一個回調:

@Override
public void onRequestPermissionsResult(int requestCode,
                                   String permissions[], int[] grantResults) 
{
  if(requestCode==0 && ActivityCompat.checkSelfPermission(MainActivity.this.getApplicationContext(), Manifest.permission.CALL_PHONE) ==
                PackageManager.PERMISSION_GRANTED) {
   Intent i = new Intent(Intent.ACTION_DIAL);
                String p = "tel:12345678";
                i.setData(Uri.parse(p));
                startActivity(i);
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM