0
我有一個名爲MainActivity的活動。 在我開始另一個活動(ContentDetails)如何保持基本活動運行?
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Uri contactUri = ContentUris.withAppendedId(People.CONTENT_URI, id);
Intent intent = new Intent(this, ContactDetails.class);
intent.setData(contactUri);
startActivity(intent);
}
而在ContactDetails.java我已經寫代碼來撥打電話。 但是當我結束通話。它完成所有的應用程序,而不是重定向到基地活動
以下是ContactDetails.java
public class ContactDetails extends Activity{
TextView nameField = null;
TextView phoneField = null;
TextView idField = null;
final Context context = this;
private Button button;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
idField = (TextView) findViewById(R.id.contact_id);
nameField = (TextView) findViewById(R.id.contact_name);
phoneField = (TextView) findViewById(R.id.contact_phone);
button = (Button) findViewById(R.id.call_button);
// add PhoneStateListener
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager;
telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:03577899456"));
startActivity(callIntent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Cursor cursor = managedQuery(getIntent().getData(), null, null, null, null);
cursor.moveToFirst();
//idField.setText(cursor.getString(cursor.getColumnIndex(People._ID)));
nameField.setText(cursor.getString(cursor.getColumnIndex(People.DISPLAY_NAME)));
phoneField.setText(cursor.getString(cursor.getColumnIndex(People.NAME)));
}
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
String LOG_TAG = "LOGGING 123";
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
Log.i(LOG_TAG, "IDLE");
if (isPhoneCalling) {
Log.i(LOG_TAG, "restart app");
// restart app
try{
// Intent intentAndroid = new Intent().setClass(this, ContactList.class);
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}catch (Exception e) {
// TODO: handle exception
}
isPhoneCalling = false;
}
}
}
}
}