重定向到设置菜单后停止意图

Mar*_*rio 2 android android-intent

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

  1. 如果我点击后退按钮(通过激活或不激活GPS)我回到我的应用程序 - 工作正常
  2. 如果我点击主页按钮,然后重新启动我的应用程序,我会直接重定向到GPS设置菜单=>我的意图仍然存在.
    问题是我不知道什么时候杀了这个意图.

这是我的代码的罪名部分:

    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.  
            }  
Run Code Online (Sandbox Code Playgroud)

我尝试了一些stopself(),finish(),在onStop(),onDestroy(),onPause()......它崩溃或什么都不做......你能帮我一把吗?

Mar*_*rio 6

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

  1. 旧行为:

    • 启动程序
    • 谷歌地图活动开始
    • 用户接受重定向到设置菜单以激活GPS
    • 显示设置菜单
    • 用户点击"主页"按钮
    • 用户重新启动应用程序=>显示设置菜单,因为它是应用程序历史堆栈中的最新意图.

启动此意图的代码是:

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
Run Code Online (Sandbox Code Playgroud)
  1. 新行为:

    • 启动程序
    • 谷歌地图活动开始
    • 用户接受重定向到设置菜单以激活GPS
    • 显示设置菜单
    • 用户点击"主页"按钮
    • 用户重新启动应用程序=>显示Google地图,而不是设置菜单,因为我已将标志FLAG_ACTIVITY_NO_HISTORY添加到重定向意图.

我的代码现在是:

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

startActivityForResult(intentRedirectionGPSSettings, 0);
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的帮助.