gpt4 book ai didi

java - 我在获取当前位置 android 时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:07 26 4
gpt4 key购买 nike

我在android(java)中的gps有问题:我在 list 中应用了权限,然后打开网络并允许gps的权限,但始终显示此消息“无法获取您的位置”我使用API​​ 27并使用我的手机j5 pro进行模拟。

https://www.youtube.com/watch?v=rg7FFUbIu9o&lc=z225styi1rzijjv5v04t1aokgqisttmwxryvfo0rplzzrk0h00410.1566142987821928该视频显示了代码源和工作

Java

    package com.example.myapplication9;
import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_LOCATION=1;

Button getlocationBtn;
TextView showLocationTxt;

LocationManager locationManager;
String latitude,longitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Add permission

ActivityCompat.requestPermissions(this,new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

showLocationTxt=findViewById(R.id.show_location);
getlocationBtn=findViewById(R.id.getLocation);

getlocationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);

//Check gps is enable or not

if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
//Write Function To enable gps

OnGPS();
}
else
{
//GPS is already On then

getLocation();
}
}
});

}

private void getLocation() {

//Check Permissions again

if (ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this,

Manifest.permission.ACCESS_COARSE_LOCATION) !=PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
}
else
{
Location LocationGps= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location LocationNetwork=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location LocationPassive=locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

if (LocationGps !=null)
{
double lat=LocationGps.getLatitude();
double longi=LocationGps.getLongitude();

latitude=String.valueOf(lat);
longitude=String.valueOf(longi);

showLocationTxt.setText("Your Location:"+"\n"+"Latitude= "+latitude+"\n"+"Longitude= "+longitude);
}
else if (LocationNetwork !=null)
{
double lat=LocationNetwork.getLatitude();
double longi=LocationNetwork.getLongitude();

latitude=String.valueOf(lat);
longitude=String.valueOf(longi);

showLocationTxt.setText("Your Location:"+"\n"+"Latitude= "+latitude+"\n"+"Longitude= "+longitude);
}
else if (LocationPassive !=null)
{
double lat=LocationPassive.getLatitude();
double longi=LocationPassive.getLongitude();

latitude=String.valueOf(lat);
longitude=String.valueOf(longi);

showLocationTxt.setText("Your Location:"+"\n"+"Latitude= "+latitude+"\n"+"Longitude= "+longitude);
}
else
{
Toast.makeText(this, "Can't Get Your Location", Toast.LENGTH_SHORT).show();
}

//Thats All Run Your App
}

}

private void OnGPS() {

final AlertDialog.Builder builder= new AlertDialog.Builder(this);

builder.setMessage("Enable GPS").setCancelable(false).setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
}).setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

dialog.cancel();
}
});
final AlertDialog alertDialog=builder.create();
alertDialog.show();
}
}
}

MainActivity xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout tools:context=".MainActivity" android:gravity="center"
android:orientation="vertical" android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/show_location"
android:textSize="20sp" android:hint="Location"/>

<Button android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/getLocation"
android:text="Get Location"/>

</LinearLayout>

list

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication9">


<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

最佳答案

试试这个:

locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

而不是你的

locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);

关于java - 我在获取当前位置 android 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57546901/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com