回到BroadcastReceiver的上一个活动?

时间:2014-03-31 01:13:12

标签: android broadcastreceiver

我想从BroadcastReceiver以编程方式“返回”。由于我无法调用finish(),我不确定如何完成

这是我的代码:

public class PhoneReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephony.getCallState() == 0) {
            // How can I go to the previous activity from here?
        }
    }
}

2 个答案:

答案 0 :(得分:0)

@Override
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent();
    i.setClass(context, YourGoBackActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}

你可以使用意图跳转到你想要的活动,这样你就可以回去"对你的活动。

答案 1 :(得分:0)

如下所示,

  public class PhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephony.getCallState() == 0) {



   runOnUiThread(new Runnable(){
     @Override
          public void run(){
          //to call activity.
          backActivity();
        }
      });
    }
}
}

public void backActivity()
  {

    Intent intent = new Intent(this, ["your back activity."].class);

    // I've tried multiple different flags to no avail.
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(intent);


    }