Androidで電話をかける方法
このチュートリアルでは、Androidで電話をかける方法と、PhoneStateListener
を介して電話の状態を監視する方法を示します。
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.
1 Androidレイアウトファイル
ボタンを表示する単純なレイアウトファイル。
ファイル:res / layout / main.xml
2. アクティビティ
以下のコードスニペットを使用して、Androidで電話をかけます。
Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:0377778888")); startActivity(callIntent);
File : MainActivity.java –ボタンが呼び出されたら、0377778888に電話をかけます。
package com.example.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マニフェスト
電話をかけるには、AndroidにCALL_PHONEの権限が必要です。
ファイル:AndroidManifest.xml
4. PhoneStateListenerの例
では、上記のアクティビティを更新して、通話状態を監視し、通話が終了したら元のアクティビティに戻ります(実際には、アクティビティを再起動するだけです)。 コメントを読んでください、それは自明であるべきです。
Note
それを実行し、logcatコンソールを参照して、PhoneStateListener
がどのように機能するかを理解します。
ファイル:MainActivity.java
package com.example.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の権限が必要です。
ファイル:AndroidManifest.xml
5. Demo
アクティビティが開始され、ボタンを表示するだけです。
ボタンがクリックされたら、0377778888に電話をかけます。
電話がハングアウトまたは終了したら、メインアクティビティを再開します。
ソースコードをダウンロード
ダウンロード–Android-Make-Phone-Call-Example.zip(16 KB)