Android-phone-calls
Android-電話
Androidは、通話用の組み込みアプリケーションを提供しますが、場合によっては、アプリケーションを介して電話をかける必要があります。 これは、適切なアクションで暗黙的なインテントを使用することで簡単に実行できます。 また、PhoneStateListenerクラスとTelephonyManagerクラスを使用して、デバイス上の一部のテレフォニー状態の変化を監視できます。
この章では、電話をかけるために使用できるアプリケーションを作成するためのすべての簡単な手順をリストします。 Androidの組み込みの電話機能を呼び出すことにより、Android Intentを使用して電話をかけることができます。 次のセクションでは、呼び出しを行うために必要なIntentオブジェクトのさまざまな部分について説明します。
インテントオブジェクト-電話をかけるアクション
*ACTION_CALL* アクションを使用して、Androidデバイスで使用可能な組み込みの電話機能をトリガーします。 以下は、ACTION_CALLアクションでインテントを作成する簡単な構文です
Intent phoneIntent = new Intent(Intent.ACTION_CALL);
ACTION_CALLの代わりに ACTION_DIAL アクションを使用できます。その場合、直接電話をかける代わりに、電話をかける前にハードコードされた電話番号を変更するオプションがあります。
インテントオブジェクト-電話をかけるためのデータ/タイプ
特定の番号91-000-000-0000で電話をかけるには、次のようにsetData()メソッドを使用してURIとして* tel:*を指定する必要があります-
phoneIntent.setData(Uri.parse("tel:91-000-000-0000"));
興味深い点は、電話をかけるために、追加のデータやデータ型を指定する必要がないということです。
例
次の例は、Android Intentを使用して特定の携帯電話番号に電話をかける方法を実際に示しています。
'_この例を試すには、最新のAndroid OSを搭載した実際のモバイルデバイスが必要です。そうでない場合は、エミュレータが動作しないことがあります。_
Step | Description |
---|---|
1 | You will use Android studio IDE to create an Android application and name it as My Application under a package com.example.saira_000.myapplication. |
2 | Modify src/MainActivity.java file and add required code to take care of making a call. |
3 | Modify layout XML file res/layout/activity_main.xml add any GUI component if required. I’m adding a simple button to Call 91-000-000-0000 number |
4 | No need to define default string constants.Android studio takes care of default constants. |
5 | Modify AndroidManifest.xml as shown below |
6 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
以下は、変更されたメインアクティビティファイル src/MainActivity.java の内容です。
package com.example.saira_000.myapplication;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonCall);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:0377778888"));
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
});
}
}
以下は res/layout/activity_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つの新しい定数を定義する res/values/strings.xml の内容です-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My Application</string>
</resources>
以下は、 AndroidManifest.xml のデフォルトのコンテンツです-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.saira_000.myapplication" >
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.saira_000.myapplication.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>
*My Application* アプリケーションを実行してみましょう。 実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、モバイルをチェックします次の画面を表示するデバイス-
今すぐ Call ボタンを使用して、以下に示すように電話をかけます-