簡體   English   中英

如何將對象從一個活動轉移到另一個活動(我想轉移DropboxAPI的mApi對象 <AndroidAuthSession> )

[英]how to transfer object from one activity to another activity (i want to transfer mApi object of DropboxAPI<AndroidAuthSession>)

我已經實現了Parceleble但仍然在putExtra中顯示錯誤。 任何人都可以幫我弄清楚代碼有什么問題以及如何糾正它。我想在另一個活動中使用mApi對象。 如果有其他方式可以請幫助我。

 public class DropboxActivity extends Activity implements Parcelable {
 private static final String TAG = "DropboxActivity";
 final static private String APP_KEY = "----------------";
 final static private String APP_SECRET = "-------------";
 final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
final static private String ACCOUNT_PREFS_NAME = "prefs";
final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";
DropboxAPI<AndroidAuthSession> mApi;
private boolean mLoggedIn;
private Button mSubmit;
private LinearLayout mDisplay;
private Button upload;
private Button download;
private ImageView mImage;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidAuthSession session = buildSession();
    mApi = new DropboxAPI<AndroidAuthSession>(session);
    setContentView(R.layout.main);
    checkAppKeySetup();
    mSubmit = (Button)findViewById(R.id.auth_button);
    mSubmit.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (mLoggedIn) {
                logOut();
            } else {
                mApi.getSession().startAuthentication(DropboxActivity.this);
            }
        }
    });
    mDisplay = (LinearLayout)findViewById(R.id.logged_in_display);
    mImage = (ImageView)findViewById(R.id.image_view);
    upload = (Button)findViewById(R.id.upload);
    upload.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        Intent intent = new Intent("upload");
        intent.putExtra("object", mApi);
        startActivity(intent);
        }
    });
    download = (Button)findViewById(R.id.download);
    setLoggedIn(mApi.getSession().isLinked());
}
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

@Override
protected void onResume() {
    super.onResume();
    AndroidAuthSession session = mApi.getSession();
    if (session.authenticationSuccessful()) {
        try {
            session.finishAuthentication();
            TokenPair tokens = session.getAccessTokenPair();
            storeKeys(tokens.key, tokens.secret);
            setLoggedIn(true);
        } catch (IllegalStateException e) {
            showToast("Couldn't authenticate with Dropbox:" + e.getLocalizedMessage());
            Log.i(TAG, "Error authenticating", e);
        }
    }
}
private void logOut() {
    mApi.getSession().unlink();
    clearKeys();
    setLoggedIn(false);
}
private void setLoggedIn(boolean loggedIn) {
    mLoggedIn = loggedIn;
    if (loggedIn) {
        mSubmit.setText("Unlink from Dropbox");
        mDisplay.setVisibility(View.VISIBLE);
    } else {
        mSubmit.setText("Link with Dropbox");
        mDisplay.setVisibility(View.GONE);
        mImage.setImageDrawable(null);
    }
}

private void checkAppKeySetup() {
    if (APP_KEY.startsWith("CHANGE") ||
            APP_SECRET.startsWith("CHANGE")) {
        showToast("You must apply for an app key and secret from developers.dropbox.com, and add them to the DBRoulette ap before trying it.");
        finish();
        return;
    }
    Intent testIntent = new Intent(Intent.ACTION_VIEW);
    String scheme = "db-" + APP_KEY;
    String uri = scheme + "://" + AuthActivity.AUTH_VERSION + "/test";
    testIntent.setData(Uri.parse(uri));
    PackageManager pm = getPackageManager();
    if (0 == pm.queryIntentActivities(testIntent, 0).size()) {
        showToast("URL scheme in your app's " +
                "manifest is not set up correctly. You should have a " +
                "com.dropbox.client2.android.AuthActivity with the " +
                "scheme: " + scheme);
        finish();
    }
}
private void showToast(String msg) {
    Toast error = Toast.makeText(this, msg, Toast.LENGTH_LONG);
    error.show();
}
private String[] getKeys() {
    SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
    String key = prefs.getString(ACCESS_KEY_NAME, null);
    String secret = prefs.getString(ACCESS_SECRET_NAME, null);
    if (key != null && secret != null) {
        String[] ret = new String[2];
        ret[0] = key;
        ret[1] = secret;
        return ret;
    } else {
        return null;
    }
}
private void storeKeys(String key, String secret) {
    // Save the access key for later
    SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
    Editor edit = prefs.edit();
    edit.putString(ACCESS_KEY_NAME, key);
    edit.putString(ACCESS_SECRET_NAME, secret);
    edit.commit();
}

private void clearKeys() {
    SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
    Editor edit = prefs.edit();
    edit.clear();
    edit.commit();
}

private AndroidAuthSession buildSession() {
    AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session;

    String[] stored = getKeys();
    if (stored != null) {
        AccessTokenPair accessToken = new AccessTokenPair(stored[0], stored[1]);
        session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE, accessToken);
    } else {
        session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE);
    }

    return session;
}
@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub

}

}

我認為讓你的活動變得可行是一個壞主意。 活動不應該通過意圖傳輸。 如果您的活動在同一個應用程序中,那么您根本不需要通過意圖進行傳輸 - 只需堅持標准的Java機制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM