活动/窗口如何获取另一个活动/窗口的文本?

时间:2011-02-11 07:29:47

标签: android

我想制作一个实时字典应用。

活动/窗口可以获取另一个活动/窗口的文本吗?

有可能吗?

P.S。此应用程序适用于手机中的所有其他应用程序,因此我无法将代码插入其他应用程序。我的意思是在没有目标应用程序的帮助下获取文本。当两个应用程序运行时,用户触摸一个应用程序,另一个应用程序获取已触摸的文本。

2 个答案:

答案 0 :(得分:2)

你可以用两种方式做到这一点。 一个是从bundle获得所需的值,第二个是使用sharedPreferences。

//使用bundle

在第一项活动中 -

    String user_id = "abcd";
    Intent intent_msg = new Intent(this, 2nd activity.class);
    Bundle bundle_msg = new Bundle();
    bundle_msg.putString("user_id", user_id);
    intent_msg.putExtras(bundle_msg);
    startActivity(intent_msg);

在第二项活动中 -

//在onCreate

    Bundle bundle = this.getIntent().getExtras();
    String user_id = bundle.getString("user_id");

//使用SharedPreferences

第一项活动 -

    Editor editor= getSharedPreferences("userId", 0).edit();
    editor.putString("userId", user_id);
    editor.commit();

第二项活动 -

    SharedPreferences pref = getSharedPreferences("userId", 1);
    String user_id = pref.getString("userId", "");

试一试。

答案 1 :(得分:0)

活动1。

//Create 1 Bundle and send with Intent
Bundle sendBundle = new Bundle();
sendBundle.putLong("value", value);

// Intent to start Activity2 và and attach sendBundble
Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtras(sendBundle);
    startActivity(i);

//exit Activity1
finish();

<强>活性2。

Bundle receiveBundle = this.getIntent().getExtras();
final long receiveValue = receiveBundle.getLong("value");

receiveValueEdit.setText(String.valueOf(receiveValue));

Intent i = new Intent(Activity2.this, Receiver.class);
i.putExtra("new value", receiveValue - 10);

谢谢Valen