2011-02-11 77 views
0

我想做一个生活字典应用程序。
活动/窗口如何获取其他活动/窗口的文本?

活动/窗口是否可以获取其他活动/窗口的文本?

这可能吗?

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

+0

您是否找到了解决方案 - 从其他应用/活动中获取感动的文本? – sberezin 2015-09-30 18:11:57

回答

2

您可以通过以下两种方式做到这一点。 一个是从bundle中获取所需值,另一个是使用sharedPreferences。

//使用束

在第一活动 -

在第二活动
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", ""); 

尝试。

+0

谢谢,看到我的P.S. – lovespring 2011-02-11 13:21:49

0

Activity1。

//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); 

感谢许茹芸

+1

你把相同的代码放在两种情况下,我想这可能是错误的... – 2011-02-11 08:33:23