如何在不扩大活动的一类使用getIntent()? [英] How to use getIntent() in a class that does not extend Activity?

查看:108
本文介绍了如何在不扩大活动的一类使用getIntent()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过从一个类玩它通过使用扩展活动的字符串:

I am trying to pass a String from a class "Play" which extends Activity by using:

Bundle data = new Bundle();
Intent i = new Intent(Play.this, Receive.class);
String category;

data.putString("key", category);
i.putExtras(data);

于是,接收类,它是一种非活性类不会扩展活动将从播放接收字符串。

Then, the "Receive" class which is a non Activity class and does not extend Activity will receive the String from "Play".

但是,当我尝试使用这个code接收数据:

But when I try to receive the data using this code:

Bundle receive = new Bundle();
String passed;

receive = getIntent().getExtras();
passed = receive.getString("key");

我的单词getIntent()得到一个错误,并建议我创造一个方法getIntent()。

I get an error on the word "getIntent()" and suggests me to create a method getIntent().

什么是可能的解决这个问题?
谢谢!

What is the possible solution to this problem? THANKS!

推荐答案

意图不是这里所必要的。你可以做这样的事情:

Intent is not nessesary here. You can just do something like this:

Play.class:

Play.class :

public String getCategory() {
    return category;
}

和在Receiver.class:

and in Receiver.class :

Play playObject = new Play();
passed = playObject.getCategory();

或者你也可以使用静态字段作为PKS提及,但它并不总是一个很好的模式。

Or you can use static field as pKs mentioned but it's not always a good pattern.

这篇关于如何在不扩大活动的一类使用getIntent()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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