電話をかけるアクティビティを開始していますが、「通話終了」ボタンを押してもアクティビティに戻りません。「通話終了」ボタンが押されたときに戻ってくる通話アクティビティを起動する方法を教えてください。これは私が電話をかける方法です:
String url = "tel:3334444";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
電話をかけるアクティビティを開始していますが、「通話終了」ボタンを押してもアクティビティに戻りません。「通話終了」ボタンが押されたときに戻ってくる通話アクティビティを起動する方法を教えてください。これは私が電話をかける方法です:
String url = "tel:3334444";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
PhoneStateListener を使用して、通話がいつ終了したかを確認します。ほとんどの場合、リスナー アクションをトリガーして呼び出しが開始されるまで待機し (PHONE_STATE_OFFHOOK から PHONE_STATE_IDLE に変更されるまで待機します)、アプリを IDLE 状態に戻すコードを記述する必要があります。
サービスでリスナーを実行して、リスナーが稼働し続け、アプリが再起動されるようにする必要がある場合があります。いくつかのサンプルコード:
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
リスナーの定義:
private class EndCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
//wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
Log.i(LOG_TAG, "OFFHOOK");
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
Log.i(LOG_TAG, "IDLE");
}
}
}
Manifest.xml
ファイルに次の権限を追加します。
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
これは、スターターからの質問に関するものです。
コードの問題は、番号を適切に渡していないことです。
コードは次のようになります。
private OnClickListener next = new OnClickListener() {
public void onClick(View v) {
EditText num=(EditText)findViewById(R.id.EditText01);
String number = "tel:" + num.getText().toString().trim();
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number));
startActivity(callIntent);
}
};
マニフェスト ファイルにアクセス許可を追加することを忘れないでください。
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
また
<uses-permission android:name="android.permission.CALL_PRIVILEGED"></uses-permission>
を使用する場合の緊急通報番号DIAL
です。
同じ問題があり、 を使用しPhoneStateListener
て通話の終了を特定することで問題を解決できましたが、さらに、finish()
で再開する前に元のアクティビティを確認startActivity
する必要がありました。そうしないと、通話ログが前に表示されます。
説明されている動作 (finish()、call、restart) を取得するために、EndCallListener が最も機能的な例であることがわかりました。リスナーがこの動作を管理するための参照を持つように、いくつかの SharedPreferences を追加しました。
私の OnClick、initialize、および EndCallListener は、アプリからの呼び出しにのみ応答します。他の呼び出しは無視されました。
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class EndCallListener extends PhoneStateListener {
private String TAG ="EndCallListener";
private int LAUNCHED = -1;
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(
myActivity.mApp.getBaseContext());
SharedPreferences.Editor _ed = prefs.edit();
@Override
public void onCallStateChanged(int state, String incomingNumber) {
String _prefKey = myActivity.mApp
.getResources().getString(R.string.last_phone_call_state_key),
_bPartyNumber = myActivity.mApp
.getResources().getString(R.string.last_phone_call_bparty_key);
int mLastCallState = prefs.getInt(_prefKey, LAUNCHED);
//Save current call sate for next call
_ed.putInt(_prefKey,state);
_ed.commit();
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(TAG, " >> RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_IDLE == state && mLastCallState != LAUNCHED ) {
//when this state occurs, and your flag is set, restart your app
if (incomingNumber.equals(_bPartyNumber) == true) {
//Call relates to last app initiated call
Intent _startMyActivity =
myActivity.mApp
.getPackageManager()
.getLaunchIntentForPackage(
myActivity.mApp.getResources()
.getString(R.string.figjam_package_path));
_startMyActivity.setAction(
myActivity.mApp.getResources()
.getString(R.string.main_show_phone_call_list));
myActivity.mApp
.startActivity(_startMyActivity);
Log.i(TAG, "IDLE >> Starting MyActivity with intent");
}
else
Log.i(TAG, "IDLE after calling "+incomingNumber);
}
}
}
これらをstrings.xmlに追加します
<string name="main_show_phone_call_list">android.intent.action.SHOW_PHONE_CALL_LIST</string>
<string name="last_phone_call_state_key">activityLpcsKey</string>
<string name="last_phone_call_bparty_key">activityLpbpKey</string>
呼び出し前のルック アンド フィールに戻る必要がある場合は、マニフェストに次のようなものを追加します。
<activity android:label="@string/app_name" android:name="com.myPackage.myActivity"
android:windowSoftInputMode="stateHidden"
android:configChanges="keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SHOW_PHONE_CALL_LIST" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
これらを「myActivity」に入れます
public static Activity mApp=null; //Before onCreate()
...
onCreate( ... ) {
...
if (mApp == null) mApp = this; //Links your resources to other classes
...
//Test if we've been called to show phone call list
Intent _outcome = getIntent();
String _phoneCallAction = mApp.getResources().getString(R.string.main_show_phone_call_list);
String _reqAction = _outcome.getAction();//Can be null when no intent involved
//Decide if we return to the Phone Call List view
if (_reqAction != null &&_reqAction.equals(_phoneCallAction) == true) {
//DO something to return to look and feel
}
...
myListView.setOnItemClickListener(new OnItemClickListener() { //Act on item when selected
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
myListView.moveToPosition(position);
String _bPartyNumber = "tel:"+myListView.getString(myListView.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Provide an initial state for the listener to access.
initialiseCallStatePreferences(_bPartyNumber);
//Setup the listener so we can restart myActivity
EndCallListener _callListener = new EndCallListener();
TelephonyManager _TM = (TelephonyManager)mApp.getSystemService(Context.TELEPHONY_SERVICE);
_TM.listen(_callListener, PhoneStateListener.LISTEN_CALL_STATE);
Intent _makeCall = new Intent(Intent.ACTION_CALL, Uri.parse(_bPartyNumber));
_makeCall.setComponent(new ComponentName("com.android.phone","com.android.phone.OutgoingCallBroadcaster"));
startActivity(_makeCall);
finish();
//Wait for call to enter the IDLE state and then we will be recalled by _callListener
}
});
}//end of onCreate()
これを使用して、たとえば onCreate() の後に myActivity で onClick の動作を初期化します。
private void initialiseCallStatePreferences(String _BParty) {
final int LAUNCHED = -1;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
mApp.getBaseContext());
SharedPreferences.Editor _ed = prefs.edit();
String _prefKey = mApp.getString(R.string.last_phone_call_state_key),
_bPartyKey = mApp.getString(R.string.last_phone_call_bparty_key);
//Save default call state before next call
_ed.putInt(_prefKey,LAUNCHED);
_ed.putString(_bPartyKey,_BParty);
_ed.commit();
}
電話番号のリストをクリックするとアクティビティが終了し、その番号に電話をかけ、通話が終了するとアクティビティに戻ることがわかります。
アプリがまだ存在している間にアプリの外部から電話をかけても、アクティビティは再開されません (最後の BParty 番号と同じでない限り)。
:)
これは私の観点からの解決策です:
ok.setOnClickListener(this);
@Override
public void onClick(View view) {
if(view == ok){
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + num));
activity.startActivity(intent);
}
もちろん、アクティビティ (クラス) 定義では、 View.OnClickListener を実装する必要があります。
これが私の例です。まず、ユーザーはダイヤルしたい番号を書き込んでから、通話ボタンを押して電話に誘導されます。通話のキャンセル後、ユーザーはアプリケーションに戻されます。これを行うには、ボタンの xml に onClick メソッド (この例では「makePhoneCall」) が必要です。また、マニフェストにアクセス許可を登録する必要があります。
マニフェスト
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
アクティビティ
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class PhoneCall extends Activity {
EditText phoneTo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_call);
phoneTo = (EditText) findViewById(R.id.phoneNumber);
}
public void makePhoneCall(View view) {
try {
String number = phoneTo.getText().toString();
Intent phoneIntent = new Intent(Intent.ACTION_CALL);
phoneIntent.setData(Uri.parse("tel:"+ number));
startActivity(phoneIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(PhoneCall.this,
"Call failed, please try again later!", Toast.LENGTH_SHORT).show();
}
}
}
XML
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/phoneNumber"
android:layout_marginTop="67dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Call"
android:id="@+id/makePhoneCall"
android:onClick="makePhoneCall"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
リスナーを使用する場合は、この権限もマニフェストに追加する必要があります。
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
通話が終了したことを確認した後、PhoneStateListener 内で次のように使用することをお勧めします。
Intent intent = new Intent(CallDispatcherActivity.this, CallDispatcherActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
CallDispatcherActivity は、ユーザーが呼び出しを開始したアクティビティです (私の場合は、タクシー サービスの配車係に)。これは Android テレフォニー アプリを一番上から削除するだけで、ここで見た醜いコードの代わりにユーザーが戻ってきます。
に戻るにはActivity
、 を聞く必要がありますTelephonyStates
。その上で、電話がアイドル状態になったらlistener
、 を送信しIntent
て再度開くことができます。Activity
少なくとも私はそうします。
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+number));
startActivity(callIntent);
**Add permission :**
<uses-permission android:name="android.permission.CALL_PHONE" />
使用してみてください:
finish();
活動の終わりに。以前のアクティビティにリダイレクトされます。
// setonclicklistener に次のコードを挿入します。
EditText et_number=(EditText)findViewById(R.id.id_of_edittext);
String my_number = et_number.getText().toString().trim();
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(my_number));
startActivity(callIntent);
// マニフェストで呼び出しの許可を与える:
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
を使用する場合は、次の aを使用して、呼び出し後に実行されるアクションをトリガーするPhoneStateListener
必要があります。を見たときにトリガーが発生すると、呼び出しの前にそれを実行することになります。状態の変化がわかるからですPHONE_STATE_IDLE
PHONE_STATE_OFFHOOK
PHONE_STATE_IDLE
PHONE_STATE_IDLE -> PHONE_STATE_OFFHOOK -> PHONE_STATE_IDLE.
これがあなたのxmlであることを追加してください:android:autoLink="phone"
手順:
1)ファイルに必要な権限を追加しManifest.xml
ます。
<!--For using the phone calls -->
<uses-permission android:name="android.permission.CALL_PHONE" />
<!--For reading phone call state-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
2) 電話の状態変化のリスナーを作成します。
public class EndCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
//wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
Intent i = context.getPackageManager().getLaunchIntentForPackage(
context.getPackageName());
//For resuming the application from the previous state
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//Uncomment the following if you want to restart the application instead of bring to front.
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
}
3)あなたのリスナーを初期化しますOnCreate
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
ただし、アプリケーションの最後の状態を再開するか、バックスタックから戻す場合は、次のように置き換えFLAG_ACTIVITY_CLEAR_TOP
ますFLAG_ACTIVITY_SINGLE_TOP
通話を開始すると、問題ないように見えます。
ただし、アプリを前面に出すには、Android 11以降とダウンの間に違いがあります。
Android 10 以下では新しいインテントを開始する必要があり、Android 11 以降では単に使用しますBringTaskToFront
コール状態 IDLE では:
if (Build.VERSION.SDK_INT >= 11) {
ActivityManager am = (ActivityManager) activity.getSystemService(Activity.ACTIVITY_SERVICE);
am.moveTaskToFront(MyActivity.MyActivityTaskId, ActivityManager.MOVE_TASK_WITH_HOME);
} else {
Intent intent = new Intent(activity, MyActivity.class);
activity.startActivity(intent);
}
アクティビティで呼び出しを行うときにこのように設定しMyActivity.MyActivityTaskId
ましたが、これが機能しない場合は、戻りたいページの親アクティビティ ページでこの変数を設定します。
MyActivity.MyActivityTaskId = this.getTaskId();
MyActivityTaskId
私のアクティビティクラスの静的変数です
public static int MyActivityTaskId = 0;
これがうまくいくことを願っています。上記のコードの使い方は少し異なります。通話に応答するとすぐにアプリを開き、ユーザーが発信者の詳細を確認できるようにします。
私もいくつかのものを設定しましAndroidManifest.xml
た:
/*Dont really know if this makes a difference*/
<activity android:name="MyActivity" android:taskAffinity="" android:launchMode="singleTask" />
および権限:
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
困ったときは質問してください。