2017-09-17 52 views
1

正如標題所說,我正在與Android超級sdk社區反犯罪應用程序。Android的優步SDK有一個OnRideRequested方法或類似的東西?

我的應用程序使用Uber SDK來獲取遊覽機會,因爲我想實現一個計時器,該計時器會在每個確定的時間將用戶位置發送給他的偏好聯繫人。所以我的疑問是,有什麼方法像'onRequestedRide'或類似的工作?

我希望用戶的應用程序要求乘坐,當他們確認並確認駕駛後,啓動計時器發送位置。

更新:

UberTracking.xml

<LinearLayout> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:background="@android:color/black"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="request widget" 
     android:textColor="@color/uber_white" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true"/> 

    <com.uber.sdk.android.rides.RideRequestButton 
     android:id="@+id/uber_button_white" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     uber:ub__style="white" /> 

</RelativeLayout> 

UberTracking.java

public class UberTracking extends AppCompatActivity implements RideRequestButtonCallback { 

private static final String DROPOFF_ADDR = "One Embarcadero Center, San Francisco"; 
private static final Double DROPOFF_LAT = 37.795079; 
private static final Double DROPOFF_LONG = -122.397805; 
private static final String DROPOFF_NICK = "Embarcadero"; 
private static final String ERROR_LOG_TAG = "UberSDK-SampleActivity"; 
private static final String PICKUP_ADDR = "1455 Market Street, San Francisco"; 
private static final Double PICKUP_LAT = 37.775304; 
private static final Double PICKUP_LONG = -122.417522; 
private static final String PICKUP_NICK = "Uber HQ"; 
private static final String UBERX_PRODUCT_ID = "a1111c8c-c720-46c3-8534-2fcdd730040d"; 
private static final int WIDGET_REQUEST_CODE = 1234; 

private static final String CLIENT_ID = BuildConfig.CLIENT_ID; 
private static final String REDIRECT_URI = BuildConfig.REDIRECT_URI; 
private static final String SERVER_TOKEN = BuildConfig.SERVER_TOKEN; 

private SessionConfiguration configuration; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_uber_tracking); 
    RideRequestButton uberButtonWhite = (RideRequestButton) findViewById(R.id.uber_button_white); 


    configuration = new SessionConfiguration.Builder() 
      .setRedirectUri(REDIRECT_URI) 
      .setClientId(CLIENT_ID) 
      .setServerToken(SERVER_TOKEN) 
      .build(); 

    validateConfiguration(configuration); 
    ServerTokenSession session = new ServerTokenSession(configuration); 

    RideParameters rideParametersForProduct = new RideParameters.Builder() 
      .setProductId(UBERX_PRODUCT_ID) 
      .setPickupLocation(PICKUP_LAT, PICKUP_LONG, PICKUP_NICK, PICKUP_ADDR) 
      .setDropoffLocation(DROPOFF_LAT, DROPOFF_LONG, DROPOFF_NICK, DROPOFF_ADDR) 
      .build(); 

    RideRequestButtonCallback rideRequestButtonCallback = new RideRequestButtonCallback() { 
     @Override 
     public void onRideInformationLoaded() { 
      Toast.makeText(UberTracking.this, "DAMN GOD", Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onError(ApiError apiError) { 
      Toast.makeText(UberTracking.this, apiError.getClientErrors().get(0).getTitle(), Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onError(Throwable throwable) { 
      Log.e("SampleActivity", "Error obtaining Metadata", throwable); 
      Toast.makeText(UberTracking.this, "Connection error", Toast.LENGTH_LONG).show(); 
     } 

    }; 

    // This button demonstrates launching the RideRequestActivity (customized button behavior). 
    // You can optionally setRideParameters for pre-filled pickup and dropoff locations. 
    RideRequestActivityBehavior rideRequestActivityBehavior = new RideRequestActivityBehavior(this, 
      WIDGET_REQUEST_CODE, configuration); 
    uberButtonWhite.setRequestBehavior(rideRequestActivityBehavior); 
    uberButtonWhite.setRideParameters(rideParametersForProduct); 
    uberButtonWhite.setSession(session); 
    uberButtonWhite.setCallback(rideRequestButtonCallback); 
    uberButtonWhite.loadRideInformation(); 



} 



@Override 
public void onRideInformationLoaded() { 
    Toast.makeText(this, "Estimates have been refreshed", Toast.LENGTH_LONG).show(); 

} 

@Override 
public void onError(ApiError apiError) { 
    Toast.makeText(this, apiError.getClientErrors().get(0).getTitle(), Toast.LENGTH_LONG).show(); 

} 

@Override 
public void onError(Throwable throwable) { 
    Log.e("SampleActivity", "Error obtaining Metadata", throwable); 
    Toast.makeText(this, "Connection error", Toast.LENGTH_LONG).show(); 
} 

/** 
* Validates the local variables needed by the Uber SDK used in the sample project 
* @param configuration 
*/ 
private void validateConfiguration(SessionConfiguration configuration) { 
    String nullError = "%s must not be null"; 
    String sampleError = "Please update your %s in the gradle.properties of the project before " + 
      "using the Uber SDK Sample app. For a more secure storage location, " + 
      "please investigate storing in your user home gradle.properties "; 

    checkNotNull(configuration, String.format(nullError, "SessionConfiguration")); 
    checkNotNull(configuration.getClientId(), String.format(nullError, "Client ID")); 
    checkNotNull(configuration.getRedirectUri(), String.format(nullError, "Redirect URI")); 
    checkNotNull(configuration.getServerToken(), String.format(nullError, "Server Token")); 
    checkState(!configuration.getClientId().equals("insert_your_client_id_here"), 
      String.format(sampleError, "Client ID")); 
    checkState(!configuration.getRedirectUri().equals("insert_your_redirect_uri_here"), 
      String.format(sampleError, "Redirect URI")); 
    checkState(!configuration.getRedirectUri().equals("insert_your_server_token_here"), 
      String.format(sampleError, "Server Token")); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == WIDGET_REQUEST_CODE && resultCode == Activity.RESULT_CANCELED && data != null) { 
     if (data.getSerializableExtra(RideRequestActivity.AUTHENTICATION_ERROR) != null) { 
      AuthenticationError error = (AuthenticationError) data.getSerializableExtra(RideRequestActivity 
        .AUTHENTICATION_ERROR); 
      Toast.makeText(UberTracking.this, "Auth error " + error.name(), Toast.LENGTH_SHORT).show(); 
      Log.d(ERROR_LOG_TAG, "Error occurred during authentication: " + error.toString 
        ().toLowerCase()); 
     } else if (data.getSerializableExtra(RideRequestActivity.RIDE_REQUEST_ERROR) != null) { 
      RideRequestViewError error = (RideRequestViewError) data.getSerializableExtra(RideRequestActivity 
        .RIDE_REQUEST_ERROR); 
      Toast.makeText(UberTracking.this, "RideRequest error " + error.name(), Toast.LENGTH_SHORT).show(); 
      Log.d(ERROR_LOG_TAG, "Error occurred in the Ride Request Widget: " + error.toString().toLowerCase()); 
     } 
    } 
} 

}

回答

0

您可以將會話添加到您的請求按鈕並具有用於您的目的的回調函數。 這是很好的解釋,例如here

祝你好運!

+0

嗨Prateek,感謝您的快速回答,我嘗試了一個虛擬烤麪包,並在應用開始時拋出敬酒,而不是在旅行開始時,米做錯了什麼?我用我的代碼更新我的初始職位。 – Poshox

+0

我不是這個SDK的專家,但不應該將此代碼放在RequestButton的onClick()方法中,而不是放在onCreate()方法中? onCreate()在應用程序啓動開始時執行,這將解釋您在應用程序中看到的症狀。 –

+0

嗨Prateek,如果我添加onClickListener應用程序不打開小部件,它只是顯示虛擬吐司,任何其他想法? – Poshox

相關問題