与传统的提示用户进入设置页面并启用定位服务然后再次返回的方法不同,我发现在一些最新的应用程序中有一种更简单的方法可以做到这一点。
参考下面的屏幕截图,它会向用户提示一个对话框,只需单击一下即可启用定位服务,并且可以在这些应用程序中运行。
我如何才能实现同样的目标?
它的工作原理类似于谷歌地图
在生成中添加依赖项。gradle文件
compile 'com.google.android.gms:play-services:8.3.0'
这个或那个
compile 'com.google.android.gms:play-services-location:10.0.1'
import android.content.Context;
import android.content.IntentSender;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import java.util.List;
public class LocationOnOff_Similar_To_Google_Maps extends AppCompatActivity {
protected static final String TAG = "LocationOnOff";
private GoogleApiClient googleApiClient;
final static int REQUEST_LOCATION = 199;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setFinishOnTouchOutside(true);
// Todo Location Already on ... start
final LocationManager manager = (LocationManager) LocationOnOff_Similar_To_Google_Maps.this.getSystemService(Context.LOCATION_SERVICE);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && hasGPSDevice(LocationOnOff_Similar_To_Google_Maps.this)) {
Toast.makeText(LocationOnOff_Similar_To_Google_Maps.this,"Gps already enabled",Toast.LENGTH_SHORT).show();
finish();
}
// Todo Location Already on ... end
if(!hasGPSDevice(LocationOnOff_Similar_To_Google_Maps.this)){
Toast.makeText(LocationOnOff_Similar_To_Google_Maps.this,"Gps not Supported",Toast.LENGTH_SHORT).show();
}
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && hasGPSDevice(LocationOnOff_Similar_To_Google_Maps.this)) {
Log.e("keshav","Gps already enabled");
Toast.makeText(LocationOnOff_Similar_To_Google_Maps.this,"Gps not enabled",Toast.LENGTH_SHORT).show();
enableLoc();
}else{
Log.e("keshav","Gps already enabled");
Toast.makeText(LocationOnOff_Similar_To_Google_Maps.this,"Gps already enabled",Toast.LENGTH_SHORT).show();
}
}
private boolean hasGPSDevice(Context context) {
final LocationManager mgr = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
if (mgr == null)
return false;
final List<String> providers = mgr.getAllProviders();
if (providers == null)
return false;
return providers.contains(LocationManager.GPS_PROVIDER);
}
private void enableLoc() {
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(LocationOnOff_Similar_To_Google_Maps.this)
.addApi(LocationServices.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
googleApiClient.connect();
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("Location error","Location error " + connectionResult.getErrorCode());
}
}).build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(LocationOnOff_Similar_To_Google_Maps.this, REQUEST_LOCATION);
finish();
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
}
}
});
}
}
}
1)根据您的意愿创建LocationRequest
LocationRequest mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000)
.setFastestInterval(1 * 1000);
2) 创建位置设置请求。生成器
LocationSettingsRequest.Builder settingsBuilder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
settingsBuilder.setAlwaysShow(true);
3)使用以下代码获取LocationSettinsACK
任务
Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(this)
.checkLocationSettings(settingsBuilder.build());
注意:LocationServices。SettinsApi
已弃用,因此请改用SettinsClient
。
4)添加OnCompleteListener
以从任务中获取结果。当任务
完成时,客户端可以通过查看LocationSettinsACK
对象中的状态代码来检查位置设置。
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response =
task.getResult(ApiException.class);
} catch (ApiException ex) {
switch (ex.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException resolvableApiException =
(ResolvableApiException) ex;
resolvableApiException
.startResolutionForResult(MapsActivity.this,
LOCATION_SETTINGS_REQUEST);
} catch (IntentSender.SendIntentException e) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
}
});
案例1:LocationSettinsStatusCodes.RESOLUTION_REQUIRED
:位置未启用,但是,我们可以通过提示用户使用对话框打开位置来要求用户启用位置(通过调用start ResolutionForResult
)。
案例2:<代码>位置设置状态代码。SETTINGS\u CHANGE\u UNAVAILABLE(设置\u更改\u不可用):不满足位置设置。但是,我们无法修复设置,因此无法显示对话框。
5)OnActivityResult
我们可以在位置设置对话框中获取用户操作。RESULT_OK
=
此对话框由LocationSettingsRequest创建。Google Play服务中提供的生成器。
您需要向应用程序的构建添加依赖项。格拉德尔:
compile 'com.google.android.gms:play-services-location:10.0.1'
然后您可以使用以下最小示例:
private void displayLocationSettingsRequest(Context context) {
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API).build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(10000 / 2);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i(TAG, "All location settings are satisfied.");
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
break;
}
}
});
}
您可以在此处找到完整的示例。
我已经用一些视频(比如10个)实现了一个回收器视图。单击任何项目都会打开一个上,其中包含所有项目显示单击的一个(来自)。之后,用户只能从那里滚动到其他项目。一切正常。 唯一的问题是当recyclerview项单击viewpager2打开的位置时滚动。 无论何时选择任何项目(第10个)。viewpager首先打开,然后滚动到(第10)个位置。我想不滚动直接打开viewpager2的第10个位置。任何
问题内容: 我有一个2的幂的整数输入(1、2、4、8等)。我希望函数不使用log()返回位位置。例如,对于上述输入,对于C#,将分别返回{0,1,2,3}。另外,如果可以在SQL中完成。 谢谢! 问题答案: 我发现执行此操作最快的代码来自Bit Twiddling Hacks网站。具体而言,基于DeBruijn序列的查找。参见http://graphics.stanford.edu/~seande
我使用下面的代码(来自这个答案)来配置要记录在WebClient请求上的头: 这很管用,但感觉有点奇怪。问题是:我是否需要像这样包含Jackson/objectMapper配置,或者是否有更简单的方法来避免Spring objectMapper配置被覆盖?
我正在windows(7.3版)上安装gvim。启动后,我希望设置变量MYVIMRC,但它没有设置。有没有理由不设定。 有一个vimrc文件,因为许多设置与默认设置不同。 我用检查了MYVIMRC的值 gvim可执行文件位于c:\workarea\Vim\vim73中,c:\workare\Vim中的*\u vimrc*(在gvim启动时读取)。启动gvim后,我可以使用 对于版本:我发现系统vi
我有下面的代码在我的网站上的下拉菜单,但当我点击它什么也没有发生。我怎样才能让它跟随href链接? 它是下拉菜单中的父项。子项工作良好。 我正在查看CSS文件中的dropdown-toggle类,由于某种原因,它被乱七八糟地排列在一个无限行上。很难读! CSS文件来自“http://getbootstrap.com/”,我有Bootstrap V3.0.3。
问题内容: 我想在不使用设置器的情况下将值插入变量。如果有可能怎么办。 这是一个例子 现在我有一个包含,和的函数。 我试图使用一种通用方法将值设置为Object(objectOfClass),而值我已经在相应的variable()中通过了()。 问题答案: 此代码未经测试。你可以试试看 要导入的类 方法