Android调用打电话(Call Phone)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Android调用打电话(Call Phone)

Android调用打电话(Call Phone)

 2014/7/21 18:59:45  lihao312  程序员俱乐部  我要评论(0)
  • 摘要:1.首先添加AndroidLayout文件File:res/layout/main.xml<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><Buttonandroid
  • 标签:android all
1.首先添加 Android Layout 文件
File : res/layout/main.xml
class="xml" name="code">
<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 listener
TelephonyManager 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;
				}
			}
		}

	}
  • TestApp.zip (1.3 MB)
  • 下载次数: 0
上一篇: 讨论下String.intern方法存在的意义 下一篇: 没有下一篇了!
发表评论
用户名: 匿名