简体   繁体   English

如何将对象从一个活动转移到另一个活动(我想转移DropboxAPI的mApi对象 <AndroidAuthSession> )

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

i have implemented Parceleble but still it shows error in the putExtra. 我已经实现了Parceleble但仍然在putExtra中显示错误。 can anyone help me to figure out what is wrong with the code and how to rectify it.i want to use mApi object in another activity . 任何人都可以帮我弄清楚代码有什么问题以及如何纠正它。我想在另一个活动中使用mApi对象。 if any other way is possible then please help me out. 如果有其他方式可以请帮助我。

 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

}

}

I think it is a bad idea to make your activity parcelable. 我认为让你的活动变得可行是一个坏主意。 Activities are not supposed to be transported via intents. 活动不应该通过意图传输。 And if your activities are inside the same application, you do not need transport via intent at all - just stick with standard java mechanisms. 如果您的活动在同一个应用程序中,那么您根本不需要通过意图进行传输 - 只需坚持标准的Java机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM