示例#1
0
  @Override
  public void onHandleIntent(Intent intent) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    final String username = prefs.getString("username", "");
    final String password = prefs.getString("password", "");

    // Check that username and password are not empty
    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {

      Toast.makeText(this, "Please update your username and password", Toast.LENGTH_LONG).show();
      return;
    }
    Log.d(TAG, "onStarted");

    YambaClient cloud = new YambaClient(username, password);
    try {
      List<YambaClient.Status> timeline = cloud.getTimeline(20);
      for (YambaClient.Status status : timeline) {
        Log.d(TAG, String.format("%s: %s", status.getUser(), status.getMessage()));
      }
    } catch (YambaClientException e) {
      Log.e(TAG, "Failed to fetch the timeline", e);
      e.printStackTrace();
    }
  }
  /** Executes on a worker thread. */
  @Override
  protected void onHandleIntent(Intent intent) {
    Log.d(TAG, "onHandleIntent");

    // Do we have yamba client?
    if (yamba == null) {
      noYamba();
      return;
    }

    // Pull the data from the cloud
    try {
      ContentValues values = new ContentValues();

      List<Status> timeline = yamba.getTimeline(20);
      for (Status status : timeline) {
        // Insert into db
        values.clear();
        values.put(StatusContract.Columns.ID, status.getId());
        values.put(StatusContract.Columns.USER, status.getUser());
        values.put(StatusContract.Columns.MESSAGE, status.getMessage());
        values.put(StatusContract.Columns.CREATED_AT, status.getCreatedAt().getTime());
        getContentResolver().insert(StatusContract.CONTENT_URI, values);

        Log.d(TAG, String.format("%s: %s", status.getUser(), status.getMessage()));
      }
    } catch (YambaClientException e) {
      e.printStackTrace();
      noYamba();
    }
  }