如何在Android中接听和挂断电话?
此示例演示了如何在Android中接听和挂断电话。
步骤1 - 在Android Studio中创建一个新项目,转到文件⇒新建项目,并填写所有必需的详细信息以创建新项目。
步骤2 - 将以下代码添加到res/layout/activity_main.xml。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Pick up and hung up calls" android:textSize="24sp" android:textStyle="bold|italic"/> </RelativeLayout>
步骤3 - 将以下代码添加到src/MainActivity.java
import android.Manifest; import android.content.pm.PackageManager; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){ if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_PHONE_STATE)){ ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1); }else{ ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1); } }else{ notify(); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){ switch (requestCode){ case 1: { if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)==PackageManager.PERMISSION_GRANTED){ Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show(); } }else{ Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show(); } return; } } } }
步骤4 – 右键单击项目,选择新建 -> Java类,并在callManager.java中添加以下代码
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class callManager extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { try{ Toast.makeText(context, "Pick up call", Toast.LENGTH_SHORT).show(); }catch (Exception e){ e.printStackTrace(); } } }
步骤5 - 将以下代码添加到androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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>
让我们尝试运行您的应用程序。我假设您已将实际的Android手机设备连接到您的计算机。要从Android Studio运行应用程序,请打开项目的活动文件之一,然后单击工具栏中的运行 图标。选择您的手机设备作为选项,然后检查您的手机设备,它将显示您的默认屏幕 -
广告