Android调用打电话(Call Phone)
16lz
2021-12-04
1.首先添加 Android Layout 文件
File : res/layout/main.xml
2. Activity
其核心代码
完整代码MainActivity.java
3.Android Manifest
添加权限
4.增加PhoneStateListener监听
MainActivity内部类
File : res/layout/main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><Button android:id="@+id/call_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/call_button"/></LinearLayout>
2. Activity
其核心代码
Intent mIntent = new Intent(Intent.ACTION_CALL);mIntent.setData(Uri.parse("tel:02133330000"));startActivity(mIntent);
完整代码MainActivity.java
package com.lance.app;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {private Button mCallButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//call button ListeneraddCallButtonListener();}/** * call button Listener */private void addCallButtonListener() {mCallButton = (Button)findViewById(R.id.call_button);mCallButton.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {Intent mIntent = new Intent(Intent.ACTION_CALL);mIntent.setData(Uri.parse("tel:02133330000"));startActivity(mIntent);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}
3.Android Manifest
添加权限
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lance.app" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <!-- 加入访问权限 --> <uses-permission android:name="android.permission.CALL_PHONE"/> <!-- 否则不能监听每个状态 --> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.lance.app.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
4.增加PhoneStateListener监听
//add call state listenerTelephonyManager telephonyManager = (TelephonyManager)getSystemService(MainActivity.TELEPHONY_SERVICE);PhoneCallListener phoneCallListener = new PhoneCallListener();telephonyManager.listen(phoneCallListener,PhoneStateListener.LISTEN_CALL_STATE);
MainActivity内部类
/** * 监听状态, 重启app * @author lance */private class PhoneCallListener extends PhoneStateListener {private final String LOG_TAG = "com.lance.app.PhoneCallListener";private boolean isPhoneCalling = false;public void onCallStateChanged(int state, String incomingNumber) {if(TelephonyManager.CALL_STATE_RINGING == state) {Log.i(LOG_TAG, "正在呼叫: "+incomingNumber);}if(TelephonyManager.CALL_STATE_OFFHOOK == state) {Log.i(LOG_TAG, "OFFHOOK");isPhoneCalling = true;}if(TelephonyManager.CALL_STATE_IDLE == state) {Log.i(LOG_TAG, "Idle");if(isPhoneCalling) {Log.i(LOG_TAG, "restart app");Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName()).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);startActivity(intent);isPhoneCalling = false;}}}}
更多相关文章
- 关于Android的selector小用法
- android监听软键盘退格(删除)事件
- android button 正常状态,按下状态
- android 去掉顶部状态栏
- Android中的一些开关
- android selector 背景选择器的使用, button (未点击,点击,选中保持
- android button 正常状态,按下状态
- android中点击事件的4种写法
- android:网路检测