requestCode 代码返回相同的值,要么更改您检查 GPS 的选项,要么不更改
我正在调用以下方法:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您无法启动活动:
以获得结果。
因此,您的 requestCode 永远不会从活动中返回。
问问自己为什么要开始这个工作以获得结果?是检查他们是否打开了GPS?如果是这样,当活动返回时,只需再次检查即可。如果是出于其他原因,我认为您不了解 startActivityForResult 的用途是什么。
相关问题:
从-location-to-my-application返回
The issue is you cannot start the Activity:
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
我明白你的意思,几天前我也遇到了同样的问题。
在这种情况下,您不应使用 onActivityResult,因为位置源设置活动永远不会设置结果代码。
为了实现你想要的,你应该在 onResume() 方法中做一些事情。一旦 GPS 位置源设置活动完成,您的 onResume() 方法将被调用。因此,您只需在 onResume 中检查 GPS 的打开或关闭即可。
请注意,首次启动应用程序时,onResume 将在 onCreate 之后调用。因此,您应该决定在 onCreate 之后或在 GPS 设置活动之后调用 onResume。
下面是示例代码
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