- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我读过不同的帖子,当显示 AlertDialog
时无法等待用户采取行动,因为它会阻塞 UI。
但是,Facebook
等应用程序显示 Gps 当前已禁用。你想启用 gps 吗? 警告对话框并等待用户按是/否。我认为可以使用 2 种不同的 Activity ,第一种仅包含 gps 警报对话框,但这似乎不正确,而且 facebook 显然不是这样做的。
谁能告诉我怎样才能做到这一点?
这是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InitializeComponents();
EnableGPSIfPossible();
ListAsync lAsync = new ListAsync(this);
lAsync .execute();
}
private void EnableGPSIfPossible()
{
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Yout GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
因此,在我的代码中,AsynTask
立即启动,无需等待用户激活 gps。 (这是正常行为)
谢谢!
最佳答案
你实际上可以做的是:
全局定位系统管理器.java :
public class GPSManager {
private Activity activity;
private LocationManager mlocManager;
private LocationListener gpsListener;
public GPSManager(Activity activity) {
this.activity = activity;
}
public void start() {
mlocManager = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
setUp();
findLoc();
} else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
activity);
alertDialogBuilder
.setMessage("GPS is disabled in your device. Enable it?")
.setCancelable(false)
.setPositiveButton("Enable GPS",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
activity.startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
public void setUp() {
gpsListener = new GPSListener(activity, mlocManager);
}
public void findLoc() {
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1,
gpsListener);
if (mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) == null)
Toast.makeText(activity, "LAST Location null", Toast.LENGTH_SHORT)
.show();
else {
gpsListener.onLocationChanged(mlocManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER));
}
}
}
全局定位系统监听器.java :
public class GPSListener implements LocationListener {
private Activity activity;
private LocationManager lm;
private int numberOfUpdates;
public static final int MAX_NUMBER_OF_UPDATES = 10;
public GPSListener(Activity activity, LocationManager lm) {
this.activity = activity;
this.lm = lm;
}
@Override
public void onLocationChanged(Location loc) {
if (numberOfUpdates < MAX_NUMBER_OF_UPDATES) {
numberOfUpdates++;
Log.w("LAT", String.valueOf(loc.getLatitude()));
Log.w("LONG", String.valueOf(loc.getLongitude()));
Log.w("ACCURACY", String.valueOf(loc.getAccuracy() + " m"));
Log.w("PROVIDER", String.valueOf(loc.getProvider()));
Log.w("SPEED", String.valueOf(loc.getSpeed() + " m/s"));
Log.w("ALTITUDE", String.valueOf(loc.getAltitude()));
Log.w("BEARING", String.valueOf(loc.getBearing() + " degrees east of true north"));
String message;
if (loc != null) {
message = "Current location is: Latitude = "
+ loc.getLatitude() + ", Longitude = "
+ loc.getLongitude();
// lm.removeUpdates(this);
} else
message = "Location null";
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
} else {
lm.removeUpdates(this);
}
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(activity, "Gps Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(activity, "Gps Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
然后从您的 Activity 中:
GPSManager gps = new GPSManager(
yourActivity.this);
gps.start();
关于Android 使用 AlertDialog : how to wait for the user to take action? 激活 gps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12044552/
我目前正在使用发现的重力脚本 here为了在我的网页上创建重力效果,我正在本地开发 atm。 我的问题是,重力效果的激活似乎是在鼠标移动时进行的,而我需要它在文档准备好时才触发。 google.cod
我正在尝试关注 Railsbridge Intallfest 并尝试将我的第一个 Rails 应用程序部署到 heroku。我不断收到以下错误消息: Gem::LoadError: Specified
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
Home-tab 是默认选中的,但是它的颜色是灰色的:( Home Bla Contact
我没有得到它的工作,我不知道为什么......遗憾的是其他问题 + 答案没有帮助。 测试设备: iPhone 6 iPad 2 相关代码: override func viewWillTransiti
我试图加载一个 View ,就像用户已经按下 UISearchBar 一样。我希望 SearchController 加载顶部的 UISearchBar 以及取消按钮。 我已经试过了: func ac
试图在 if whiteDotDist < centerRadius - whiteDotRadius 时获取代码执行它下面的所有代码都是事件的,并且当它下面的代码被执行时它再次变为非事件状态直到if
我正在使用 anaconda python。所以每次,在我的 mac 终端中,我输入终端命令: source /Users/mylaptop/anaconda/bin/activate /Users/
在我的 Angular 项目中,我有这种代码: this.swUpdate.available.subscribe(() => { ... }); 它工作正常,但给了我关于 activated 被
我想弄清楚 Julia 包是如何工作的,因为我喜欢容器化环境。我真的很挣扎。 在 python 中,我会做类似 conda create env --name ds 的事情创建环境然后安装容器化包我会
我的宏中有一些代码如下 ChDir File_pth Workbooks.Open filename:= File_pth & "\" & open_tkt Workbooks.Open filena
长话短说,我有两张纸,一张是“原始数据”,另一张是“结果”。我试图让结果表从“原始数据”表的每第七行中提取文本或数字,因此“结果”中的 A1 将是原始数据中的 A1,“结果”中的 A2 将是“原始数据
我不知道如何做到这一点,或者我是否可以做到这一点。我有一个 jQuery UI Accordion,多个部分,每个部分包含多个 anchor 标记,每个 anchor 标记都有一个唯一的字符串 id。
我不敢相信我还没有找到任何关于此的文档,但我想知道如何命令键盘激活并接收来自它的输入。我可以找到在编辑文本字段时操作弹出键盘的所有示例。谢谢 最佳答案 您还可以使用 UIKeyInput 协议(pro
我正在尝试为我的 Electron 应用程序生成NSIS安装程序的日志。为此,我创建了一个文件'logging.nsh'来定义LogSet和LogText宏。 以下是logging.nsh文件的代码:
几周前,我开始使用 typescript 和 knockoutJS,我有一个具体的问题,但我有解决方案,它太丑了,我无法忍受,但无法从中得到任何更好的东西,有太多代码需要粘贴,但我会尽力描述我的问题:
当我尝试激活我的虚拟环境时收到此错误即源 ~/edu-venv/bin/activate -bash: /home/vagrant/edu-venv/bin/activate: No such fil
要创建触发器,似乎必须发布它才能生效。但是发布需要对“协作”分支进行 PR,这意味着我们甚至在测试触发器是否实际工作之前就必须创建一个 PR,并且还必须创建多个后续 PR,直到我们获得正确的触发器。
我是最近的 IntelliJ Idea 用户,我不知道如何启用 Hibernate。当我右键单击我的项目时,Hibernate 不会出现在“添加框架支持”菜单中(实际上我唯一可以选择的技术是 Groo
要创建触发器,似乎必须发布它才能生效。但是发布需要对“协作”分支进行 PR,这意味着我们甚至在测试触发器是否实际工作之前就必须创建一个 PR,并且还必须创建多个后续 PR,直到我们获得正确的触发器。
我是一名优秀的程序员,十分优秀!