protected void onMessage(Context context, Intent intent) {
String msg = intent.getStringExtra("payload");
String newspushid1= intent.getStringExtra("payload1");
gc.setnews_pushid(newspushid1);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
BackgroundAlert bg = new BackgroundAlert(mNotificationManager, msg);
bg.onReceive(getApplicationContext(), intent);
}
在这里我获得了msg和Push id(分别是helloworld和101)的值,我希望在另一个活动中获得此值。
if (gdata.getPush_message() != null) {
String push_id_value = gdata.getnews_pushid();
Log.e("CATID", "ID-->" + push_id_value);
}
我已经应用此代码来获取值。来自GCMIntentService类,但是我得到了相同的值push_id_value,getPush_message在另一个活动中,这是hello world我无法在另一个活动中获得值101请检查我的代码告诉我解决方案。< / p>
答案 0 :(得分:1)
在您当前的活动中,
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
然后在新的Activity中,检索这些值:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
使用此技术将值从一个Activity传递到另一个Activity。
答案 1 :(得分:1)
创建一个Constant
类,如:
public class Constant {
public static String PAYLOAD = "";
public static String PAYLOAD_1 = "";
}
现在,您可以在Activity1
中设置此值
Constant.PAYLOAD = intent.getStringExtra("payload");
Constant.PAYLOAD_1= intent.getStringExtra("payload1");
现在,在Activity2
中获取此值,如:
String push_id_value = Constant.PAYLOAD;
String push_id_value1 = Constant.PAYLOAD_1;
和/或偏好转到我的答案:Android. How to save user name and password after the app is closed?