重定向到设置菜单后停止意图 [英] Stop an intent after a redirection to setting menu

查看:58
本文介绍了重定向到设置菜单后停止意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个使用googleMaps视图上的GPS位置的应用程序.我检查是否激活了GPS功能,如果没有激活,我会将用户重定向到设置菜单.

I'm currently developing an app which uses GPS location on googleMaps view. I check if GPS feature is activated, and if not I redirect the user to the setting menu.

  1. 如果我单击后退"按钮(通过激活GPS或未激活GPS),我将返回我的应用-可以正常运行
  2. 如果我单击主页"按钮,然后重新启动我的应用程序,则会直接将我重定向到GPS设置菜单=>我的意图仍在.
    问题是我不知道什么时候杀死这个意图.
  1. If I click on Back button (by activating or not the GPS) I come back to my app - works fine
  2. If I click on Home button, and then relaunch my app, I am directly redirected to the GPS setting menu => my intent is still on.
    The problem is that I do not know when kill this intent.

这是我代码的缩影部分:

Here is the incriminated part of my code:

    Button btnLocateMe = (Button)findViewById(Rsendlocationinfo.id.locateme);
    btnLocateMe.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Context context = getApplicationContext();

            objgps = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

            //Check GPS configuration
            if ( !objgps.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {  

                //Warn the user that its GPS is not activated
                Toast gpsActivationCheck = Toast.makeText(context, "GPS deactivated - Touch to open the GPS settings.", Toast.LENGTH_LONG);
                gpsActivationCheck.show();

                //Open the GPS settings menu on the next onTouch event
                //by implementing directly the listener method - dirt manner
                mapView.setOnTouchListener(new OnTouchListener() {
                    public boolean onTouch(View v, MotionEvent event) {  

                        //And here is my problem - How to kill this process
                        startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);  

                        return false;
                    }
                });      
            }
            else {  
                //Location acquisition.  
            }  

我在onStop(),onDestroy(),onPause()中尝试了stopstop(),finish()...它崩溃了或什么也没做... 你能帮我一下吗?

I tried some stopself(), finish(), in onStop(), onDestroy(), onPause()... It crashes or does not do anything... Could you please give me a hand ?

推荐答案

好,我终于找到了.
我将尝试清楚地解释行为上的差异:

Ok, I have finally found.
I will try to explain the difference in the behavior clearly:

  1. 旧行为:

  1. Old behavior:

  • 启动应用程序
  • Google地图活动开始
  • 用户接受重定向到设置菜单以激活GPS
  • 显示设置菜单
  • 用户单击主页"按钮
  • 用户重新启动该应用程序 =>显示设置菜单,因为它是应用程序历史记录堆栈中的最新意图.
  • Launch application
  • Google maps activity starts
  • User accepts redirection to the setting menu to activate GPS
  • Setting menu displayed
  • User click on "Home" button
  • User relaunch the application => the setting menu is displayed, as it is the latest intent in the history stack of the application.

启动此意图的代码是:

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);

  1. 新行为:

  1. New behavior:

  • 启动应用程序
  • Google地图活动开始
  • 用户接受重定向到设置菜单以激活GPS
  • 显示设置菜单
  • 用户单击主页"按钮
  • 用户重新启动该应用程序 =>显示Google地图,而不显示设置菜单,因为我已将FLAG_ACTIVITY_NO_HISTORY标志添加到重定向意图中.
  • Launch application
  • Google maps activity starts
  • User accepts redirection to the setting menu to activate GPS
  • Setting menu displayed
  • User click on "Home" button
  • User relaunch the application => The Google maps is displayed, and not the setting menu, because I have added the flag FLAG_ACTIVITY_NO_HISTORY to the redirection intent.

我的代码现在是:

Intent intentRedirectionGPSSettings = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intentRedirectionGPSSettings.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

startActivityForResult(intentRedirectionGPSSettings, 0);

我试图解释得很好. manifest.xml中的标签"android:noHistory(true)"适用于活动,但是标志FLAG_ACTIVITY_NO_HISTORY是一种不错的选择,因为它适用于意图.

I tried to explain well. The tag "android:noHistory(true)" in the manifest.xml works for activities, but the flag FLAG_ACTIVITY_NO_HISTORY is a good alternative, as it works for intents.

感谢您的帮助.

这篇关于重定向到设置菜单后停止意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆