android中的隐含意图问题

时间:2016-02-12 07:11:29

标签: java android android-intent

在我的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();

当我使用以下代码从activityback button时,我想重新启动location service

Intent myIntent = new Intent(
    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);

感谢。

1 个答案:

答案 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