我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
您可以使用intent对象在活动之间发送数据。假设您有两个活动,即FirstActivity和SecondActivity。
内部第一活动:
使用意图:
i = new Intent(FirstActivity.this,SecondActivity.class);
i.putExtra("key", value);
startActivity(i)
内部第二活动
Bundle bundle= getIntent().getExtras();
现在,您可以使用不同的bundle类方法获取通过Key从FirstActivity传递的值。
例如。bundle.getString(“key”)、bundle.get-Double(“key“)、bundle.getInt(“key”)等。
其他回答
/*
* If you are from transferring data from one class that doesn't
* extend Activity, then you need to do something like this.
*/
public class abc {
Context context;
public abc(Context context) {
this.context = context;
}
public void something() {
context.startactivity(new Intent(context, anyone.class).putextra("key", value));
}
}
我最近发布了Vapor API,这是一个jQuery风格的Android框架,它使类似这样的所有任务都更加简单。如前所述,SharedPreferences是实现此目的的一种方法。
VaporSharedPreferences实现为Singleton,因此这是一个选项,在VaporAPI中,它有一个重载的.put(…)方法,因此您不必明确担心您提交的数据类型-前提是它受支持。它也很流畅,因此您可以拨打电话:
$.prefs(...).put("val1", 123).put("val2", "Hello World!").put("something", 3.34);
它还可以选择自动保存更改,并在后台统一读写过程,因此您不需要像在标准Android中那样显式检索编辑器。
或者,您可以使用意向。在VaporAPI中,您还可以对VaporContent使用可链接的overload.put(…)方法:
$.Intent().put("data", "myData").put("more", 568)...
如其他答案中所述,将其作为附加项传递。您可以从“活动”中检索额外内容,此外,如果您正在使用VaporActivity,这将自动为您完成,因此您可以使用:
this.extras()
要在切换到的“活动”的另一端检索它们。
希望有人对此感兴趣:)
要访问所有活动中的会话id,必须将会话id存储在SharedPreference中。
请参见下面我用于管理会话的类,您也可以使用该类。
import android.content.Context;
import android.content.SharedPreferences;
public class SessionManager {
public static String KEY_SESSIONID = "session_id";
public static String PREF_NAME = "AppPref";
SharedPreferences sharedPreference;
SharedPreferences.Editor editor;
Context mContext;
public SessionManager(Context context) {
this.mContext = context;
sharedPreference = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
editor = sharedPreference.edit();
}
public String getSessionId() {
return sharedPreference.getString(KEY_SESSIONID, "");
}
public void setSessionID(String id) {
editor.putString(KEY_SESSIONID, id);
editor.commit();
editor.apply();
}
}
//Now you can access your session using below methods in every activities.
SessionManager sm = new SessionManager(MainActivity.this);
sm.getSessionId();
//below line us used to set session id on after success response on login page.
sm.setSessionID("test");
更新注意,我已经提到了SharedPreference的使用。它有一个简单的API,可以跨应用程序的活动访问。但这是一个笨拙的解决方案,如果您传递敏感数据,则会带来安全风险。最好使用意图。它包含大量重载方法,可用于在活动之间更好地传输许多不同的数据类型。看看intent.putExtra。这个链接很好地展示了putExtra的用法。
在活动之间传递数据时,我的首选方法是为相关活动创建一个静态方法,其中包含启动意图所需的参数。这样就可以方便地设置和检索参数。所以它看起来像这样
public class MyActivity extends Activity {
public static final String ARG_PARAM1 = "arg_param1";
...
public static getIntent(Activity from, String param1, Long param2...) {
Intent intent = new Intent(from, MyActivity.class);
intent.putExtra(ARG_PARAM1, param1);
intent.putExtra(ARG_PARAM2, param2);
return intent;
}
....
// Use it like this.
startActivity(MyActvitiy.getIntent(FromActivity.this, varA, varB, ...));
...
然后,您可以为预期活动创建意图,并确保您拥有所有参数。你可以将碎片调整为。上面的一个简单示例,但你明白了。
第一项活动:
Intent intent = new Intent(getApplicationContext(), ClassName.class);
intent.putExtra("Variable name", "Value you want to pass");
startActivity(intent);
第二项活动:
String str= getIntent().getStringExtra("Variable name which you sent as an extra");