打完电话后重启应用程序

时间:2012-11-20 09:03:37

标签: android call restart

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

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

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

1 个答案:

答案 0 :(得分:0)

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