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;
                }

            }
        }
    }

}
4

1 回答 1

3

实际上,我认为您必须使用

startActivityForResult(callIntent, <RESULT_OK>);

代替

startActivity(callIntent);

并在您的ContactDetails活动中覆盖。 onActivityResult()

试试这个,让我知道会发生什么..

同样从您的代码中,我建议您不要使用标志i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);作为清除当前活动状态任务的方法。

于 2012-11-24T12:47:13.100 回答