因此,我正在构建此电话应用程序。在MainActivity
中,它会使用人的名字呼叫另一个活动phoneActivity
,当您单击一个名字时,它将拉出带有该人的电话号码的电话应用程序。通话结束后,我想让它返回主活动。我被困在那里。我认为原因是我在通话结束或排序后没有结束活动。
我确实在日志中看到它已变为OFFHOOK。通话结束后,我需要终止活动吗?
public class PhoneActivity extends AppCompatActivity {
Intent dialIntent;
//need to keep track coz first state is IDLE
boolean offHook = false;
String[] initialItems = {"Zod","Lida"};
HashMap<String, String> map = new HashMap<>();
/* Check the Phone State to move back to the calling page */
private class EndCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
int prevState = state;
Log.i("State of Call: ", Integer.toString(state));
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i("PHONE ", "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("PHONE ", "OFFHOOK"); // I see this in logs, what needs to be done here?
//phone has passed the inital state
offHook = true;
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
Log.i("PHONE ", " Enter IDLE");
//finish();
if((prevState==TelephonyManager.CALL_STATE_OFFHOOK)){
Log.i("PHONE", "prev OFFHOOK " + Integer.toString(state));
prevState=state;
//Answered Call which is ended
finish();
}
if((prevState==TelephonyManager.CALL_STATE_RINGING)){
Log.i("PHONE", "prev RINGING " + Integer.toString(state));
prevState=state;
//Rejected or Missed call
}
/*
if ( offHook ) {
//finish();
Log.i("PHONE","IDLE OFFHOOK");
Intent goBack = new Intent(getApplicationContext(), MainActivity.class);
//Initial phone state is false
offHook = false;
startActivity(goBack);
} */
}
}
}
public void openDialer(String person) {
Log.i("openDialer: ", "inside opendialer");
//Uri u = Uri.parse("tel:" + "xxxxxxxxx");
Uri u = Uri.parse("tel:" + map.get(person));
dialIntent = new Intent(Intent.ACTION_DIAL,u);
//dialIntent.setAction(Intent.ACTION_DIAL);
PhoneActivity.EndCallListener callListener = new PhoneActivity.EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
startActivity(dialIntent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone);
/*
HashMap<String, String> map = new HashMap<>();
map.put("Zod","+88888888888");
map.put("Lida","+140888888888");
final List<String> myList = map;
*/
map.put("Zod","+18188888888");
map.put("Lida","+14088888888");
final List<String> myList = Arrays.asList(initialItems);
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,myList){
//ArrayAdapter arrayAdapter = new ArrayAdapter<HashMap>(this, android.R.layout.simple_list_item_1,myList){
public View getView(int position, View convertView, ViewGroup parent) {
//Cast the listView each item as textView
TextView item = (TextView) super.getView(position, convertView, parent);
// Set the item text style to bold
item.setTypeface(item.getTypeface(), Typeface.BOLD);
// Set size to 36
item.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 36);
return item;
}
};
ListView listView = findViewById(R.id.listView);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("Item clicked: ", myList.get(position));
Toast.makeText(getApplicationContext(),"Calling " + myList.get(position), Toast.LENGTH_LONG).show();
openDialer(myList.get(position));
//System.out.println( myList.get(position) );
}
});
TextView textViewEdit = findViewById(R.id.textViewEdit);
textViewEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialer("Dummy");
}
});
}
}
答案 0 :(得分:0)
finish()
之后呼叫startActivity(dialIntent);
将关闭您的phoneActivity
并返回到您的MainActivity
。