简体   繁体   English

在Android中-有人关闭应用程序时如何启动活动?

[英]In android - how to I make an activity launch when someone closes an app?

I'd like to add an activity to my app asking users to rate it, but I'd rather have it only launch when they are exiting out of the app using the back button (so that it doesn't interfere with usefulness). 我想在我的应用程序中添加一个活动,要求用户对其进行评分,但是我希望它仅在用户使用后退按钮退出应用程序时才启动(以免影响实用性)。 I have a few apps that if I exit out by repeatedly hitting the back button, I get a toast that says 'Tap back to exit ______' so I'm pretty sure that this is possible. 我有一些应用程序,如果我通过反复按“后退”按钮退出,我会得到一个吐司,上面写着“点按以退出______”,因此,我很确定这是可能的。

You can start another activity using intent on your onBackPressed method. 您可以使用onBackPressed方法上的intent启动另一个活动。 With this approach you can fire an intent to google play. 使用这种方法,您可以激发Google Play的意图。 But i think it would be frustrating for user. 但是我认为这会让用户感到沮丧。 In my opinion better way of achieving what you want is using some kind of popup. 我认为,实现所需目标的更好方法是使用某种弹出窗口。 This library might help you out. 可能会帮助您。

As far as I see I would do it like in the following code(not tested, just a snippet): 据我所知,我会像下面的代码那样做(未经测试,只是一个片段):

boolean exitApp = false;

@Override
public void onBackPressed() {
    if (exitApp && (isBackStackEntryEmpty())) {
        super.onBackPressed();
        return;
    }
    //put here a dialog check
    checkDialog();  
}

private void checkDialog(){
  //show a dialog with an ok or a cancel.
  //in the cancel it will ask you for a second press.
  //if ok is pressed go to rate else, cancel, see below.
  this.exitApp = true;
  showToast();
  postDelayed();
}

private Handler handler = new Handler();
private boolean isBackStackEntryEmpty(){
   return isBackStackEntryEmpty;// check for that in code.
}

private void postDelayed()
{
   handler.postDelayed(new Runnable()
   {
      @Override
      public void run() {
           this.exitApp = false;                      
      }
   }, 3000);
}

public void showToast(){
   Toast.makeText(this, "Please click again to exit", Toast.LENGTH_SHORT).show();
}


Similar: 类似:
Clicking the back button twice to exit an activity 单击返回按钮两次以退出活动

Just a thought...record whether or not a user was shown the popup asking for a review in shared preferences so that it prevents the app from asking again. 只是一个想法...记录是否向用户显示弹出窗口,要求在共享首选项中进行审阅,以防止应用再次询问。 But you can't force people to rate your app, only choose not to show them the reminder again 但是您不能强迫人们对您的应用进行评分,只能选择不再次向他们显示提醒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM