2016-02-12 44 views
2

在我的Android應用程序中,我使用implicit intent來打開location service。我想在打開service後重新啓動應用程序。我如何檢查location service的結果並重新啓動我的appAndroid中的隱含意圖問題

我用下面的代碼

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 buttonlocation service

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

謝謝。

+0

發佈您的logcat。 –

+0

logcat中沒有問題。我想在啓動屏幕打開位置服務之後加載MainActivity。但它仍然在飛濺活動,同時按位置服務頁面上的按鈕。然後關閉應用程序並再次打開,現在它轉到MainActivity。 – krishna

回答

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); 
} 

希望它能幫助。

注:I假設您的SplashScreenlaunchMode不是singleTask