How can I share code between my application and my service?

Denny

For the sake of this question, I've replaced some logic with comments and simplified the layous. But the main purpose of the question should be the same.

So I have an activity view with a button and a textview.

layout_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click me"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

Code behind MainActivity.java

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // retrieve data from internet and display in the textview
            findViewById(R.id.textView).setText(response);
        }
    });

Now with this library, I can display the layout_main with a service in a floating window outside of the activity. See demo gif on the github page. With another button on my xml I start the service.

startService(new Intent(MainActivity.this, MyService.class));

Demo code from the github page:

MyService.java

@Override
public void onCreate() {
    super.onCreate();

    windowManagerContainer = new WindowManagerContainer(this);
    chatHeadManager = new DefaultChatHeadManager<String>(this, windowManagerContainer);
    chatHeadManager.setViewAdapter(new ChatHeadViewAdapter<String>() {

        @Override
        public View attachView(String key, ChatHead chatHead, ViewGroup parent) {
            View cachedView = viewCache.get(key);
            if (cachedView == null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                View view = inflater.inflate(R.layout.layout_main, parent, false);
                // 'problem' here
                cachedView = view;
                viewCache.put(key, view);
            }
            parent.addView(cachedView);
            return cachedView;
        }
...

But now the problem is, I have to create duplicate code in the service to handle the network requests and display the text. It is basically the same code in the activity.

So my question is, can I somehow share the 'code-behind' to use for my activity and my service or do I have to create a duplicate of the code from my activity?

Denny

I can indeed just put my view as a paramater of a class to handle both views there

For my activity:

View view = getLayoutInflater().inflate(R.layout_main, null);
setContentView(view);
new ViewHandler(MainActivity.this, view);
...

For my service:

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_main, false);
new ViewHandler(MyService.this, view);

The ViewHandler:

public ViewHandler(Context context, View view) {
    super(context, view);
    // get ids from the view and attach handlers or do other things with it
    Button button = (Button) view.findViewById(R.id.button);
    ....

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I decouple my application from my membership service?

From Dev

How can I share a picture from my application to Whatsapp?

From Dev

How can I share my clipboard between two X servers?

From Dev

How can I share my clipboard between two X servers?

From Dev

Can I share my tarsnap key file between machines?

From Dev

can I share my SSH keys between WSL and Windows?

From Dev

How can I share my hosted app's code in Google App Engine

From Dev

How can I share build code script for all my gradle projects (not just subprojects)

From Dev

How i share my Wifi using application swift in iOS

From Dev

How can I stop my old service?

From Dev

how can i make my linux service trigger my signal?

From Dev

How do I get a route in my code for my WebAPI application

From Dev

How can I retrieve the injector for my application?

From Dev

how can i evaluate my spark application

From Dev

How can I keep my application in the foreground?

From Dev

How do I include a forms application in my service application?

From Dev

How can I get my ad banner to the bottom of my application?

From Dev

How can I share my website in progress to partner?

From Dev

How can I share my internet connection from windows to ubuntu?

From Dev

How can I share my WiFi connection through Bluetooth?

From Dev

How can I share my WiFi connection through Bluetooth?

From Dev

How can I share my form with few controllers?

From Dev

How can I share my visualisations in Kibana-4

From Dev

How can I create a wifi hotspot and share my localhost across

From Dev

How can I share a windows folder with my virtualbox ubuntu installation?

From Dev

How can I share my desktop using Skype?

From Dev

How can I share my screen with Skype for Linux 5.3?

From Dev

How can I use an application class in order to start a service and use it in my activities?

From Dev

How can I organize my Java code?

Related Related

  1. 1

    How can I decouple my application from my membership service?

  2. 2

    How can I share a picture from my application to Whatsapp?

  3. 3

    How can I share my clipboard between two X servers?

  4. 4

    How can I share my clipboard between two X servers?

  5. 5

    Can I share my tarsnap key file between machines?

  6. 6

    can I share my SSH keys between WSL and Windows?

  7. 7

    How can I share my hosted app's code in Google App Engine

  8. 8

    How can I share build code script for all my gradle projects (not just subprojects)

  9. 9

    How i share my Wifi using application swift in iOS

  10. 10

    How can I stop my old service?

  11. 11

    how can i make my linux service trigger my signal?

  12. 12

    How do I get a route in my code for my WebAPI application

  13. 13

    How can I retrieve the injector for my application?

  14. 14

    how can i evaluate my spark application

  15. 15

    How can I keep my application in the foreground?

  16. 16

    How do I include a forms application in my service application?

  17. 17

    How can I get my ad banner to the bottom of my application?

  18. 18

    How can I share my website in progress to partner?

  19. 19

    How can I share my internet connection from windows to ubuntu?

  20. 20

    How can I share my WiFi connection through Bluetooth?

  21. 21

    How can I share my WiFi connection through Bluetooth?

  22. 22

    How can I share my form with few controllers?

  23. 23

    How can I share my visualisations in Kibana-4

  24. 24

    How can I create a wifi hotspot and share my localhost across

  25. 25

    How can I share a windows folder with my virtualbox ubuntu installation?

  26. 26

    How can I share my desktop using Skype?

  27. 27

    How can I share my screen with Skype for Linux 5.3?

  28. 28

    How can I use an application class in order to start a service and use it in my activities?

  29. 29

    How can I organize my Java code?

HotTag

Archive