我试图找到一种方法,在用户自动启用位置服务后如何将用户返回到我的应用,而无需用户按下后退按钮。 我检查是否启用了位置服务:
if( !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
alertForNoLocationEnabled();
}else {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new MyLocationListener());
}
private void alertForNoLocationEnabled() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.network_not_found_title); // network not found
builder.setMessage(R.string.network_not_found_message); // Want to enable?
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
toggleSwipeRefreshLayoutsOff();
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
toggleSwipeRefreshLayoutsOff();
}
});
AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
并且在用户打开我想要的位置服务后,我自动将用户带回我的应用程序而不是按后退按钮。 请任何想要分享它的人。
答案 0 :(得分:2)
这可能有用。
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
toggleSwipeRefreshLayoutsOff();
AsyncTask.execute(new Runnable() {
@Override
public void run() {
long fortySecondsFromNow = System.currentTimeMillis() + 40*1000
while((System.curremtTimeMillis < fortySecondsFromNow)
&& !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))) {
Thread.sleep(300);
}
if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER){
Intent intent = new Intent(context, YourActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
//Do what u want
}
});
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}