当前位置: 代码迷 >> Android >> Android调用通电话(Call Phone)
  详细解决方案

Android调用通电话(Call Phone)

热度:24   发布时间:2016-04-28 04:42:18.0
Android调用打电话(Call Phone)
1.首先添加 Android Layout 文件
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;		@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);				//call button Listener		addCallButtonListener();	}		/**	 * 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);			}		});	}		@Override	public 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;				}			}		}	}
  相关解决方案