antoshka
antoshka

Reputation: 13

How to get Activity from Service

I have a need to make a soft push in different activities, but within the same application.

I made the decision to do this by showing the application on top of other applications. To create a floating widget, I copied the code from here.

Now all I have to do is click. To do this, I created a method like this:

void simulateEventDown (Activity yourActivity, float x, float y)

    {
// Press
        MotionEvent e = MotionEvent.obtain (SystemClock.uptimeMillis (), SystemClock.uptimeMillis (),
                MotionEvent.ACTION_DOWN, x, y, 0);
        yourActivity.dispatchTouchEvent (e);
// Release here
        MotionEvent e2 = MotionEvent.obtain (SystemClock.uptimeMillis (), SystemClock.uptimeMillis (),
                MotionEvent.ACTION_UP, x, y, 0);
        yourActivity.dispatchTouchEvent (e2);

    }

It accepts an Activity, but the Service does not contain an Activity, as strange as it might seem. I'm new to Android and Java, so any advice is welcome! Maybe some of you have come across this or have another solution to this problem, share, I will be glad to every comment

P.S. I do not have access to the activity in which it is necessary to click (This is a library).

UPDATE
I found out that in addition to Activity, this can be done with View, only then, clicking will occur only inside this View

Upvotes: 0

Views: 54

Answers (1)

gpuser
gpuser

Reputation: 1183

Try to use the Bind Service Mechanism with any activity. You can do it according to your current open activity

Read this https://developer.android.com/guide/components/bound-services

Another way you can use broadcast receiver from service to activity where you can update your view by sending intent with data you want

Upvotes: 1

Related Questions