在我的Android应用程序中,我正在使用implicit intent
启用location service
。我想在打开service
后重新启动应用程序。如何检查location service
的结果并重新启动app
。
我使用以下代码
AlertDialog.Builder dialog = new AlertDialog.Builder(Splash.this);
dialog.setMessage("Location not enabled");
dialog.setPositiveButton("open settings",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface paramDialogInterface,int paramInt)
{
// TODO Auto-generated method stub
Intent myIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
});
dialog.setNegativeButton("cancel",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt)
{
// TODO Auto-generated method stub
finish();
}
});
dialog.show();
当我使用以下代码从activity
按back button
时,我想重新启动location service
。
Intent myIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
感谢。
答案 0 :(得分:0)
使用startActivityForResult
,
@Override
public void onClick(View v)
{
startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 1);
}
并检查以onActivityResult
确认,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==RESULT_OK)
{
if(resultCode == RESULT_OK && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Button bt = (Button)findViewById(R.id.turnGPS);
bt.setVisibility(View.INVISIBLE);
this.finish();
}
else if(resultCode == RESULT_CANCELED)
{
Toast.makeText(getBaseContext(), "No GPS device or service enabled", Toast.LENGTH_LONG+Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
希望它有所帮助。
注意:我假设您的 SplashScreen 的launchMode
不是singleTask
。