requestCode 代码返回相同的值,要么更改您检查 GPS 的选项,要么不更改

发布于 2024-10-22 02:55:37 字数 683 浏览 10 评论 0原文

我正在调用以下方法:

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

但在 onActivityResultas 下面的方法中

    @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;
            }
         }  
     }

,“requestCode”和“resultCode”返回相同的值,无论您“打开”gps 设置还是返回而不执行“打开”。我不想调用 userData();当用户没有打开“打开”GPS 设置时。请调查此事

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 don't want to call userData(); when user didn't turn "on" gps setting. Please look into matter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩饰不了的爱 2024-10-29 02:55:37

问题是您无法启动活动:

android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS

以获得结果。

因此,您的 requestCode 永远不会从活动中返回。

问问自己为什么要开始这个工作以获得结果?是检查他们是否打开了GPS?如果是这样,当活动返回时,只需再次检查即可。如果是出于其他原因,我认为您不了解 startActivityForResult 的用途是什么。

相关问题:
从-location-to-my-application返回

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

破晓 2024-10-29 02:55:37

我明白你的意思,几天前我也遇到了同样的问题。

在这种情况下,您不应使用 onActivityResult,因为位置源设置活动永远不会设置结果代码。

为了实现你想要的,你应该在 onResume() 方法中做一些事情。一旦 GPS 位置源设置活动完成,您的 onResume() 方法将被调用。因此,您只需在 onResume 中检查 GPS 的打开或关闭即可。

请注意,首次启动应用程序时,onResume 将在 onCreate 之后调用。因此,您应该决定在 onCreate 之后或在 GPS 设置活动之后调用 onResume。

下面是示例代码

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

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
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文