如何从一个类发送值到不同的类别(一类以上) [英] how to send value from one class into different classes(more than one class)

查看:111
本文介绍了如何从一个类发送值到不同的类别(一类以上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有值 strUser的,我想这个值发送到多个不同的类,因为这个值将在5类URL中使用,我知道送价值一类只使用方法 Intent.putExtra 波纹管:

I have value strUser and KEY ,I want to send this value to multiple different classes because this value will be used in 5 classes for url, I know the way to send value to one class only using Intent.putExtra as bellow :

Intent policy= new Intent(LoginActivity.this,EpolicyListPolis.class);
        policy.putExtra("etUser",strUser);
        policy.putExtra("key",KEY);
        startActivity(policy);

我怎么能一次发送此值,以多个不同的课?我可以使用共享prefences ..?怎么我写在课堂上共享prefences的方式,我的目标类?

How can I send this value to multiple different classes at a time? can i use SharedPrefences..? how the way i write sharedPrefences in class and my destination class?

推荐答案

您可以使用的共享preferences 的。

有关存储的值到共享preferences

SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("etUser", strUser);
editor.putString("key",KEY);
editor.commit();

就在之前调用这些行粘贴 startActivity(政策);

和得到的值共享preferences

SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
String etUser1 = settings.getString("etUser", null);
String key1 = settings.getString("key", null);

和粘贴这些线在你想 etUser1 键1 其中。您可以在任何活动访问此共享preferences 值。如果你不能,请采取<一看href=\"http://stackoverflow.com/questions/3624280/how-to-use-shared$p$pferences-in-android-to-store-fetch-and-edit-values\">here.它可以帮助你。

And paste these lines at where you want etUser1 and key1. You can access this SharedPreferences value in any Activity. If you cant please take a look on here. It may help you.

我希望这会帮助你。

这篇关于如何从一个类发送值到不同的类别(一类以上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆