Get the App
SLTechnology News&Howtos  ›  Development  › 

How to hang up the phone with Android

Shulou Source: shulou.com Published: 2022-06-01 14:28:25 10月03日 Update

This article mainly introduces Android how to hang up the phone related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe everyone will have some gains after reading this Android how to hang up the phone article, let's take a look at it.

1. Android Dynamic Request Permission

First, you need to statically apply for phone permissions in AndroidManifest.xml, otherwise you cannot dynamically apply for permissions. The following code positions cannot be misplaced (outside the application):

Then, write the logic of dynamic permission application in java code (this is the official website)

public void requestPower() {//Determine whether permission has been granted if (ContextCompat.checkSelfPermission(this, Manifest.permission. above phone permission character) != PackageManager.PERMISSION_GRANTED) { //This method returns true if the application previously requested this permission but the user refused the request. if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest. permission. above telephone permission character)) {//Here you can write a dialog box to explain to the user why you want to apply for permission, and in the dialog box confirm the key to apply for permission again. It returns false if the user selects "Don't ask again." } else { //Apply permission. The string array contains one or more permissions to apply for. 1 is the return parameter of the permission application result. You can find the application result in onRequestPermissionsResult. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission. Phone characters above,}, 1); } 2. Create an AIDL file ITelephony package name must be (com.android.internal.telephony)// ITelephony.aidl package com.android.internal.telephony; / Declare any non-default types here with import statements interface ITelephony { boolean endCall(); void answerRingCall (); }3. Write java code to hang up the phone private void endCall() { try { //Get the hidden API through reflection, and get the Class object of the hidden class Class clazz = Class.forName("android.os.ServiceManager"); //Get Method object Method method = clazz.getMethod("getService", String.class); //call method to get iBinder object IBinder ibinder = (IBinder) method.invoke(null, Context.TELEPHONY_SERVICE); ITelephony iTelephony = ITelephony.Stub.asInterface(ibinder); iTelephony.endCall();//End call } catch (Exception e) { e.printStackTrace(); } }

Remember to add phone privileges dynamically! Otherwise, hanging up the phone would not be possible.

The content of this article on "Android how to hang up the phone" is introduced here, thank you for reading! I believe everyone has a certain understanding of the knowledge of "Android how to hang up the phone." If you still want to learn more knowledge, please pay attention to the industry information channel.

Tags: Permissions phone dynamic character object code user knowledge content dialog box method article result dialogue value location again writing parameters multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology MySQL NVidia Huawei