Stop an intent after a redirection to setting menu

2019-04-14 15:52发布

问题:

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. 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.  
            }  

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

回答1:

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

  1. Old behavior:

    • 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.

The code to launch this intent was:

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
  1. New behavior:

    • 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.

My code is now:

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

startActivityForResult(intentRedirectionGPSSettings, 0);

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.

Thank you for help.



回答2:

The Location Settings Activity has now become part of your application/tasks when you invoked it from your calling logic. As far as Android is concerned this was the last screen from "your" application before your app became inactive. So, when you go back into the application again it will go back to that Activity automatically.

This is the normal and documented behavior. If you don't want that Activty to be there when you go back then try calling "finishActivity()" ... this only works for those Activities that were started via the startActivityForResult() method.



回答3:

use android:launchMode="singleTask" in AndroidManifest