Để thực hiện cuộc gọi với ứng dụng Android cần sử dụng perrmission
answer Hoàng Nhã Uyên · Hoàng Nhã Uyên 07:43 09/02/2015 sử dụng PhoneStateListener để xem khi nào cuộc gọi kết thúc. rất có thể bạn sẽ cần kích hoạt các hành động của người nghe để chờ cuộc gọi bắt đầu (đợi cho đến khi thay đổi từ PHONE_STATE_OFFHOOK thành PHONE_STATE_IDLE một lần nữa) và sau đó viết một số mã để đưa ứng dụng của bạn trở lại trạng thái IDLE. bạn có thể cần chạy trình nghe trong một dịch vụ để đảm bảo nó luôn hoạt động và ứng dụng của bạn được khởi động lại. một số mã ví dụ: EndCallListener callListener = new EndCallListener(); TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);Định nghĩa người nghe: 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"); } } }Trong Manifest.xmltệp của bạn thêm quyền sau: 105 hữu ích 4 bình luận chia sẻ answer Võ Nam Phi · Võ Nam Phi 11:11 13/06/2012 Điều này liên quan đến câu hỏi của Starter. Vấn đề với mã của bạn là bạn không chuyển đúng số. Mã phải là: 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); } };Đừng quên thêm quyền trong tệp kê khai. hoặc là cho số khẩn cấp trong trường hợp DIALđược sử dụng. 49 hữu ích 4 bình luận chia sẻ answer Huỳnh Ngọc Sương · Huỳnh Ngọc Sương 07:45 09/02/2015 Chúng tôi có cùng một vấn đề và quản lý để giải quyết nó bằng cách sử dụng a PhoneStateListenerđể xác định khi cuộc gọi kết thúc, nhưng ngoài ra chúng tôi phải thực finish()hiện hoạt động ban đầu trước khi bắt đầu lại startActivity, nếu không thì nhật ký cuộc gọi sẽ ở phía trước nó. 24 hữu ích 5 bình luận chia sẻ answer Tạ Vinh Gia · Tạ Vinh Gia 11:02 04/11/2011 Tôi đã tìm thấy EndCallListener là ví dụ chức năng nhất, để có được hành vi được mô tả (finish (), gọi, khởi động lại) Tôi đã thêm một vài SharedPreferences để Listener có một tham chiếu để quản lý hành vi này. OnClick, khởi tạo và EndCallListener của tôi chỉ trả lời các cuộc gọi từ ứng dụng. Các cuộc gọi khác bị bỏ qua. 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); } } }thêm những thứ này vào chuỗi và một cái gì đó như thế này trong Bản kê khai của bạn nếu bạn cần quay lại giao diện trước khi gọi và đặt những thứ này vào 'myActivity' của bạn 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()sử dụng điều này để khởi tạo hành vi cho onClick của bạn trong myActivity, ví dụ như sau 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(); }Bạn sẽ thấy rằng việc nhấp vào danh sách các số điện thoại của bạn sẽ kết thúc hoạt động của bạn, thực hiện cuộc gọi đến số đó và trở về hoạt động của bạn khi cuộc gọi kết thúc. Thực hiện cuộc gọi từ bên ngoài ứng dụng của bạn trong khi nó vẫn không khởi động lại hoạt động của bạn (trừ khi nó giống như số BParty cuối cùng được gọi). :) 13 hữu ích 1 bình luận chia sẻ answer Vũ Ngọc Danh · Vũ Ngọc Danh 11:23 10/05/2011 bạn có thể sử dụng startActivityForResult () 7 hữu ích 2 bình luận chia sẻ answer Đỗ Ðức Kiên · Đỗ Ðức Kiên 12:57 05/11/2009 Đây là giải pháp theo quan điểm của tôi: 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); }Tất nhiên trong định nghĩa Activity (class), bạn phải triển khai View.OnClickListener. 6 hữu ích 0 bình luận chia sẻ answer Huỳnh Quang Khánh · Huỳnh Quang Khánh 14:24 14/04/2015 Dưới đây là ví dụ của tôi, đầu tiên người dùng sẽ ghi vào số mà anh ấy / cô ấy muốn quay số và sau đó nhấn nút gọi và được chuyển hướng đến điện thoại. Sau khi hủy cuộc gọi, người dùng sẽ được gửi trở lại ứng dụng. Để làm được điều này, nút cần phải có phương thức onClick ('makePhoneCall' trong ví dụ này) trong xml. Bạn cũng cần phải đăng ký sự cho phép trong bảng kê khai. Biểu hiện Hoạt động 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 |