我有一个名为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;
}
}
}
}
}
答案 0 :(得分:3)
实际上,我认为你必须使用
startActivityForResult(callIntent, <RESULT_OK>);
而不是
startActivity(callIntent);
在 ContactDetails 活动中覆盖 onActivityResult()
。
试试这个,让我知道发生了什么......
同样来自您的代码我建议您不要使用Flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
因为它清除当前的活动状态任务。