开发者

requestCode code is returning same value either change the you check option for gps or not

开发者 https://www.devze.com 2023-02-16 23:35 出处:网络
I am calling the following method: Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);

I am calling the following method:

Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, GPS_LOC);

but on onActivityResultas below method

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != -1) {
           switch (requestCode) {
              case GPS_LOC:
                userData();
               break;
            }
         }  
     }

it's "requestCode" and "resultCode" returning same value either you "on" the gps setting or returning without doing "on". I d开发者_Go百科on't want to call userData(); when user didn't turn "on" gps setting. Please look into matter


The issue is you cannot start the Activity:

android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS

for a result.

Therefore your requestCode is never returned from the activity.

Ask yourself why you want to start this for a result? Is it to check if they have turned on the GPS? If it is so, when the activity returns just check it again. If it is for another reason I don't think you grasp what startActivityForResult's use is.

Related question: getting-back-from-location-to-my-application


I understand what you mean and I had the same issue several days ago.

In you case, you should not use onActivityResult since the location source setting activity will never set the result code.

In order to achieve what you want, you should do something in your onResume() method. Once the GPS location source setting activity finished, your onResume() method will be called. So you just check the GPS on or off in onResume.

Pay attention, onResume will be called after onCreate when you first start the app. So you should decide onResume is called after onCreate or after the GPS setting activity.

Below is the a sample code

 void onCreate(){
    //do something 
    Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);}

 void onResume(){
    if (firstTime){
      // do something
       firstTime = false;
    }
    else{ // it's from GPS setting activity
     //check your gps again with your logic here
    }
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号