전화를 걸기위한 활동을 시작하고 있지만 ‘통화 종료’버튼을 눌렀을 때 내 활동으로 돌아 가지 않습니다. ‘통화 종료’버튼을 눌렀을 때 되돌아 오는 통화 활동을 시작하려면 어떻게해야합니까? 이것이 내가 전화하는 방법입니다.
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
호출이 끝나는 시간을 식별하기 위해 a 를 사용하여 문제를 해결할 수 있었지만 finish()
다시 시작하기 전에 원래 활동을 수행해야했습니다 startActivity
. 그렇지 않으면 호출 로그가 앞에 있습니다.
답변
EndCallListener가 가장 기능적인 예라는 것을 발견했습니다. (finish (), call, restart) 설명 된 동작을 얻기 위해 몇 가지 SharedPreferences를 추가하여 리스너가이 동작을 관리하기위한 참조를 갖도록했습니다.
내 OnClick, 초기화 및 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()
이것을 사용하여 myActivity에서 onClick의 동작을 초기화하십시오 (예 : onCreate () 후)
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 번호와 동일하지 않은 경우).
🙂
답변
startActivityForResult ()를 사용할 수 있습니다
답변
이것은 내 관점에서 해결책입니다.
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" />