Finish()或moveTaskToBack()不起作用

时间:2015-11-11 18:38:16

标签: android android-activity background task activity-finish

我试图将活动放在后台或者只是将其杀死。我在runnable中创建了活动,因为我需要在它打开之前等待2秒:

case TelephonyManager.CALL_STATE_RINGING:
            long user_logged = AppSettings.getLoggedUserSQLiteID(context);
            if (user_logged != 0) {
                final Handler h = new Handler();
                Runnable r1 = new Runnable() {
                    @Override
                    public void run() {
                        String phoneNr = inte.getStringExtra("incoming_number");
                        Intent phonecall = new Intent(ctx, PhoneCall.class);
                        phonecall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        phonecall.putExtra("phone_number", phoneNr);
                        ctx.startActivity(phonecall);
                    }
                };
                h.postDelayed(r1, 2000); // 1 second delay
            }
break;

在按钮中,我指定了在点击时运行的方法,例如android:onClick="acceptCall"

public void acceptCall(View v) {

    try {
        Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
    } catch (IOException e) {
        // Runtime.exec(String) had an I/O problem, try to fall back
        String enforcedPerm = "android.permission.CALL_PRIVILEGED";
        Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
                Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
                        KeyEvent.KEYCODE_HEADSETHOOK));
        Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
                Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
                        KeyEvent.KEYCODE_HEADSETHOOK));

        this.sendOrderedBroadcast(btnDown, enforcedPerm);
        this.sendOrderedBroadcast(btnUp, enforcedPerm);

        super.finish();

    }
}

要完成或将其放在后台,我尝试了以下几行代码:

super.finish();
super.moveTaskToBack(true);
this.finish();
this.moveTaskToBack(true);

然而,它们似乎都不适合我。谁能帮我理解为什么?

1 个答案:

答案 0 :(得分:0)

在默认情况下要杀死活动,我们应该使用:

MyActivity是您的活动。

MyActivity.this.finish();

//尝试在此代码行之后终止您的活动:

ctx.startActivity(phonecall);