Androidで電話をかける方法
このチュートリアルでは、Androidで電話をかける方法と、「PhoneStateListener」で電話の状態を監視する方法を紹介します。
P.SこのプロジェクトはEclipse 3.7で開発され、Android 2.3.3.
でテストされています
1 Androidレイアウトファイル
ボタンを表示するためのシンプルなレイアウトファイル。
File:res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <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/buttonCall" android:layout__width="wrap__content" android:layout__height="wrap__content" android:text="call 0377778888"/> </LinearLayout>
2.活動
以下のコードスニペットを使用してAndroidで電話をかけます。
Intent callIntent = new Intent(Intent.ACTION__CALL); callIntent.setData(Uri.parse("tel:0377778888")); startActivity(callIntent);
File:MainActivity.java
– ボタンが呼び出されたら、電話を0377778888にします。
package com.mkyong.android; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button button; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.buttonCall); //add button listener button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent callIntent = new Intent(Intent.ACTION__CALL); callIntent.setData(Uri.parse("tel:0377778888")); startActivity(callIntent); } }); } }
3 Android Manifest
電話をかけるには、Androidに
CALL__PHONE
権限が必要です。
<uses-permission android:name="android.permission.CALL__PHONE"/>
File:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mkyong.android" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10"/> <uses-permission android:name="android.permission.CALL__PHONE"/> <application android:icon="@drawable/ic__launcher" android:label="@string/app__name" > <activity android:label="@string/app__name" android:name=".MainActivity" > <intent-filter > <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
4. PhoneStateListenerの例
さて、上記のアクティビティを更新し、電話の状態を監視し、電話が終了したときに元のアクティビティに戻ります(実際にはアクティビティを再開するだけです)。コメントを読む、それは自明でなければならない。
File:MainActivity.java
package com.mkyong.android; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { final Context context = this; private Button button; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.buttonCall); //add PhoneStateListener PhoneCallListener phoneListener = new PhoneCallListener(); TelephonyManager telephonyManager = (TelephonyManager) this .getSystemService(Context.TELEPHONY__SERVICE); telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN__CALL__STATE); //add button listener button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent callIntent = new Intent(Intent.ACTION__CALL); callIntent.setData(Uri.parse("tel:0377778888")); startActivity(callIntent); } }); } //monitor phone call activities private class PhoneCallListener extends PhoneStateListener { private boolean isPhoneCalling = false; String LOG__TAG = "LOGGING 123"; @Override public void onCallStateChanged(int state, String incomingNumber) { if (TelephonyManager.CALL__STATE__RINGING == state) { //phone ringing Log.i(LOG__TAG, "RINGING, number: " + incomingNumber); } if (TelephonyManager.CALL__STATE__OFFHOOK == state) { //active Log.i(LOG__TAG, "OFFHOOK"); isPhoneCalling = true; } if (TelephonyManager.CALL__STATE__IDLE == state) { //run when class initial and phone call ended, //need detect flag from CALL__STATE__OFFHOOK Log.i(LOG__TAG, "IDLE"); if (isPhoneCalling) { Log.i(LOG__TAG, "restart app"); //restart app Intent i = getBaseContext().getPackageManager() .getLaunchIntentForPackage( getBaseContext().getPackageName()); i.addFlags(Intent.FLAG__ACTIVITY__CLEAR__TOP); startActivity(i); isPhoneCalling = false; } } } } }
Androidマニフェストファイルを再度更新する。 `PhoneStateListener`は、
READ
PHONE
STATE
のアクセス権が必要です。
<uses-permission android:name="android.permission.READ__PHONE__STATE"/>
File:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mkyong.android" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10"/> <uses-permission android:name="android.permission.CALL__PHONE"/> <uses-permission android:name="android.permission.READ__PHONE__STATE"/> <application android:icon="@drawable/ic__launcher" android:label="@string/app__name" > <activity android:label="@string/app__name" android:name=".MainActivity" > <intent-filter > <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
5.デモ
アクティビティが開始されました。ボタンを表示するだけです。
ボタンをクリックすると、0377778888に電話がかかります。
電話がハングアウトまたは終了したら、メインアクティビティを再開します。
ソースコードをダウンロードする
ダウンロードする –
Android-Make-Phone-Call-Example.zip
(16 KB)
参考文献
PhoneStateListener Javadoc]。
http://developer.android.com/reference/android/telephony/TelephonyManager.html
[Android
TelephonyManager Javadoc]。
http://developer.android.com/reference/android/content/Intent.html
[Android
インテントJavadoc]
リンク://タグ/アンドロイド/[アンドロイド]リンク://タグ/電話 – コール/[電話]