0

我在通话后返回我的应用程序时遇到问题,因为通话结束后它会将我发送到主屏幕而不是我的应用程序。

public class ButtonView extends FrameLayout  {
private static final String TAG = ButtonView.class.getSimpleName();
private final View mButtonView;

private Server mServer;
final Context context = getContext();

public ButtonView(Context context, AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    mButtonView = inflater.inflate(R.layout.input, this);
}

.
    .
    . 
    . 
    .
    .
public void call()  {
    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager) getContext()
            .getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener,
            PhoneStateListener.LISTEN_CALL_STATE);

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:048598077"));
        getContext().startActivity(callIntent); 


}


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
                Context appContext = context.getApplicationContext();
                Intent i = appContext.getPackageManager().getLaunchIntentForPackage(appContext.getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);

                isPhoneCalling = false;
            }
        }
    }
}

有人知道问题是什么吗?或任何其他方式在通话后重新启动应用程序?

4

1 回答 1

0

如果您使用的是模拟器,请在真实设备而不是模拟器上测试您的应用程序。您不需要 PhoneStateListener 并手动启动您的应用程序。只需在真实设备上测试,它将自动启动您的应用程序。

于 2012-11-20T10:17:57.477 回答