- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个 Android 应用程序,它可以借助 A-GPS 帮助找到 GPS 位置,从设置中打开 GPS 后,我想运行进度对话框,直到纬度和经度大于零。我是新手,我编写了以下无法正常工作的代码。任何帮助将不胜感激
谢谢
package com.example.gpsutility;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class GPSActivity extends Activity implements OnClickListener {
Button search,exit;
TextView location_tv;
GpsService gps;
ProgressDialog progressDialog = null;
double latitude,longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps);
search = (Button)findViewById(R.id.search_button);
exit = (Button)findViewById(R.id.exit_button);
location_tv =(TextView)findViewById(R.id.textview2);
gps = new GpsService(this);
search.setOnClickListener(this);
exit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==search)
{
progressDialog = ProgressDialog.show(this, "", "getting location...", true);
new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if(latitude<=0 && longitude<=0)
{
do{
latitude =gps.getLatitude();
longitude = gps.getLongitude();
// location_tv.setText("Location is -\nLat: "+latitude+ "\nLong: "+longitude );
}while(latitude<=0 && longitude<=0);
location_tv.setText(" Location is-\nLat:"+latitude+"\nLong: "+longitude );
progressDialog.dismiss();
}else{
location_tv.setText(" Location is-\nLat:"+latitude+"\nLong: "+longitude );
Log.e("latlong", latitude + ""+longitude);
progressDialog.dismiss();
}
}
});
}
}).start(); }
if(v==exit)
{
finish();
}
}
}
我使用的GPS SERVICE类是
package com.example.gpsutility;
public class GpsService extends Service implements LocationListener {
private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
public static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=10;
public static final long MIN_TIME_BW_UPDATES=1000*60*1;
protected LocationManager locationManager;
public GpsService(Context context)
{
this.mContext=context;
getLocation();
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public Location getLocation()
{
try {
locationManager =(LocationManager)
mContext.getSystemService(LOCATION_SERVICE); isGPSEnabled=locationManager.isProviderEnabled(locationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER);
if(!isGPSEnabled || !isNetworkEnabled)
{
showSettingsAlert();
}
else {
this.canGetLocation=true;
if(isNetworkEnabled)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// Setting Icon to Dialog
//alertDialog.setIcon(R.drawable.delete);
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
}
最佳答案
试试这个。
ProgressDialog progressBar;
int progressStatus = 0;
progressBar = new ProgressDialog(this);
progressBar.setCancelable(false);
progressBar.setMessage("getting location...");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
progressBarStatus = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
progressBarStatus = getLocation();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
if (progressBarStatus >= 100) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.dismiss();
}
}
}).start();
private int getLocation()
{
GpsService gps = new GpsService(this)
latitude = gps.getLatitude();
longitude = gps.getLongitude();
if(latitude > 0 && longitude > 0)
return 100;
else
return 0;
}
此外,您确定需要将其用作服务吗?或者这只是一个名字,它真的只是一个简单的类?如果它是一个简单的类并且您只是通过按钮或任何操作拉取位置,则可以使用上面的代码。因为当你在 android 中使用 Service 时,你是在后台运行它,如果你想在 X 间隔后重复执行进程并在你通过代码或使用 stopSelf() 停止它时停止,你需要创建一个处理程序。如果它真的是一项服务,您需要为您的 Activity 创建一个广播监听器,并使用该服务发送广播并根据广播更新您的 UI。我希望这有帮助。干杯!
关于android - 运行进度对话框,直到在 Android 应用程序中找不到 GPS i.d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19395020/
我需要在 JavaScript 中的笛卡尔坐标和球坐标之间进行转换。我在论坛上浏览了一下,没有找到我要找的东西。 现在我有这个: this.rho = sqrt((x*x) + (y*y) + (z*
有没有matrix3d可以像这样把矩形变成梯形的?我知道常规的 2d 矩阵变换只能以平行四边形结束,因为您只能有效地倾斜和旋转。 div { width: 300px; height:
关于这个例子(d3.j radial tree node links different sizes),我想知道是否可以在 d3.js 中混合径向树和直线树。 对于我的 jsFiddle 示例:htt
我尽量把标题写得最好,但我不确定如何准确描述这里发生的事情,所以请随时更正。 我想使用 › 直 Angular 引号 (›) 而不是 > 直 Angular 引号 (>),虽然 › 字符比 > 短,但
我正在尝试使用 CSS 创建一个具有圆边的矩形棱柱,如下图所示。 到目前为止,我已经指定了顶部和底部的边界半径。问题是我不知道如何让另一边的左右边缘向内 curl 。因此,拐 Angular 处不应有
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 去年关闭。 社区去年审查了是否重
首先,我刚刚开始学习 HTML 和 CSS。 我想如何使用这段代码: https://codepen.io/martinjkelly/pen/vEOBvL .container { width:
我是一名优秀的程序员,十分优秀!