开发者

How can a Activity/Window get the text of another Activity/Window?

开发者 https://www.devze.com 2023-02-09 18:08 出处:网络
I want make a live dictionary app. Can a Activity/Window get the text of another Activity/Window ? 开发者_开发问答

I want make a live dictionary app.

Can a Activity/Window get the text of another Activity/Window ?

开发者_开发问答

Is it possible ?

P.S. this app will works for all other apps in the phone, so I cannot insert code to other apps. I mean get text without the help of the target apps. When two apps running, user touch one app and the other app get the text that been touched.


you can do it in two way. one is getting required value from bundle and second is by using sharedPreferences.

// using bundle

in 1st activity -

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

in 2nd activity -

// inside onCreate

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

// using SharedPreferences

1st activity -

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

2nd activity -

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

try it.


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

Activity2.

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

Thanks Valen

0

精彩评论

暂无评论...
验证码 换一张
取 消