许可后 GPS 位置不起作用(SDK 23)android studio
GPS location after permission not working (SDK 23) android studio
我有这个 class 我创建的,我需要把我最后的位置放在哪里?
public class DescriptionActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks {
TextView t;
Button up;
GoogleApiClient mGoogleApiClient;
LocationManager locationManager;
LocationListener locationListener;
LocationServices mLastLocation;
private static final int PERMS_REQUEST_CODE=123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hr__description);
Toolbar toolbar = (Toolbar) findViewById(R.id.hr_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_clear_white_24dp);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
t = (TextView) findViewById(R.id.test_boaz);
up=(Button)findViewById(R.id.upload);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
.build();
if(hasPermission())
{
getGPS();
}
else {
requestPerms();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case PERMS_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// All good!
} else {
Log.d("bobbbbbbbbbbbbbbbbbb","boazboabzobaob");
}
break;
}
}
private void getGPS() {
up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private boolean hasPermission(){
int res=0;
String[] permissions=new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};
for(String params:permissions)
{
res=checkCallingOrSelfPermission(params);
if(!(res==PackageManager.PERMISSION_GRANTED)) {
return false;
}
}
return true;
}
private void requestPerms(){
String[] permissions=new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
requestPermissions(permissions,PERMS_REQUEST_CODE);
ActivityCompat.requestPermissions(this, permissions, PERMS_REQUEST_CODE);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
}
}
@Override
public void onConnectionSuspended(int i) {
}
}
我尝试激活 GPS,但它总是要我检查权限
如何解决?
查看了互联网上的许多示例,我成功地获得了激活 GPS 设备的对话框,但是当我尝试获取位置时,我遇到了问题
When you want to make a connection to one of the Google APIs provided
in the Google Play services library (such as Google Sign-In, Games, or
Drive), you need to create an instance of GoogleApiClient ("Google API
Client"). The Google API Client provides a common entry point to all
the Google Play services and manages the network connection between
the user's device and each Google service.
- 自动管理您与 Google Play 服务的连接。
对任何 Google 执行同步和异步 API 调用
播放服务。
手动管理您与 Google Play 服务的连接
极少数情况下这是必要的。
Add this to manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.gms.location.sample.basiclocationsample" >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>
Add this code in your activity
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build()
@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
In onCreate Method in your activity
我有这个 class 我创建的,我需要把我最后的位置放在哪里?
public class DescriptionActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks {
TextView t;
Button up;
GoogleApiClient mGoogleApiClient;
LocationManager locationManager;
LocationListener locationListener;
LocationServices mLastLocation;
private static final int PERMS_REQUEST_CODE=123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hr__description);
Toolbar toolbar = (Toolbar) findViewById(R.id.hr_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_clear_white_24dp);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
t = (TextView) findViewById(R.id.test_boaz);
up=(Button)findViewById(R.id.upload);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
.build();
if(hasPermission())
{
getGPS();
}
else {
requestPerms();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case PERMS_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// All good!
} else {
Log.d("bobbbbbbbbbbbbbbbbbb","boazboabzobaob");
}
break;
}
}
private void getGPS() {
up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private boolean hasPermission(){
int res=0;
String[] permissions=new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};
for(String params:permissions)
{
res=checkCallingOrSelfPermission(params);
if(!(res==PackageManager.PERMISSION_GRANTED)) {
return false;
}
}
return true;
}
private void requestPerms(){
String[] permissions=new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
requestPermissions(permissions,PERMS_REQUEST_CODE);
ActivityCompat.requestPermissions(this, permissions, PERMS_REQUEST_CODE);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
}
}
@Override
public void onConnectionSuspended(int i) {
}
}
我尝试激活 GPS,但它总是要我检查权限 如何解决? 查看了互联网上的许多示例,我成功地获得了激活 GPS 设备的对话框,但是当我尝试获取位置时,我遇到了问题
When you want to make a connection to one of the Google APIs provided in the Google Play services library (such as Google Sign-In, Games, or Drive), you need to create an instance of GoogleApiClient ("Google API Client"). The Google API Client provides a common entry point to all the Google Play services and manages the network connection between the user's device and each Google service.
- 自动管理您与 Google Play 服务的连接。
对任何 Google 执行同步和异步 API 调用 播放服务。
手动管理您与 Google Play 服务的连接 极少数情况下这是必要的。
Add this to manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.gms.location.sample.basiclocationsample" >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>
Add this code in your activity
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build()
@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
In onCreate Method in your activity