简体   繁体   中英

How can I create a background service that functions are accessible from a concurrent application in android?

I'm looking to start a service when a button is pressed in my application.

This service however, will need to be running in the background and maintain a SQLiteConnection object without closing/restarting.

I'll need to access functions in the service, these functions will query a database that is kept on a server over the network using liteSync.

What I've tried;

<service
            android:name=".NetworkStuff.Server"
            android:enabled="true"
            android:exported="true"
            android:stopWithTask="false"></service>

I've tried creating and starting a service, however when i switch activities the Service object is destroyed. I've tried using other methods of maintaining an SQLiteConnection object to no avail such as the following code.

public class MyApplication extends Application{
    private SQLiteConnection data;

    public SQLiteConnection getData() {
       return data;
    }

    public void setData(SQLiteConnection data) {
        this.data = data;
    }

The only way I've correctly been able to use the SQLiteConnection object is by doing everything in one activity (insert,select,update,delete). So from that I looked into running a concurrent application (that will never close) that contains all the above query functions, then a client application would call these functions without needing to even see the SQLiteConnection object. From that I was lead to a background service that does exactly that.

All the examples I've seen and tried don't seem to run outside of my application (I can't see them in "Running Services" through android settings)

How to call methods of a Service from activity?

https://github.com/MysticMagic/ServiceAndSqlite

How can i initiate a service class that will handle the previously mentioned without having to close/restart it that I can access from an application?

--In reply to comment--

I'm fine with communicating with the database, but yes i'd like a continuously running service that will communicate with a database hosted on a server. A bit more info; io.liteglue.SQLiteConnection; I'm using a library called litesync with that all I need is a SQliteConnection object to be maintained by a continuously running service. This service will need to perform statements (just run a function like the one below)

public ArrayList<String> getData(String statement) {
        ArrayList<String> results = new ArrayList<>();

        try {
            SQLiteStatement mystatement;

            try {
                mystatement = mydbc.prepareStatement(statement);
            } catch (SQLException ex) {

                return results;
            }

            while (mystatement.step()) {
                results.add(mystatement.getColumnTextNativeString(0));

                }
            mystatement.dispose();

        }
        catch (SQLException ex) {

            return results;
        }

        return results;
    }

The main objective here is to use that same SQLiteConnection object (which holds the address to the server's database)

I'm already able to run all the functions I need however it disconnects randomly and stops updating on the main part. Therefore I'm looking for a single continuous background service that will handle all calls to the SQLiteConnection object that is never closed and acts all in one class.

You have to create an IntentService (that has its own Thread) that lives in a Separated Process (I think this step could be optional, but I suggest to let it live in its own Process) and then use the "AIDL inter-process-communication technique" to communicate between Activities/Apps and this Service. It's impossible to write all the code here, but I'll give you few hints:

  • create the IntentService
  • create an AIDL file that expones all methods you will want to use from this Service
  • starting from Android 6 all "permanent" Services should display a notification while running, so you have to make that Service in foreground
  • do a "startService()"/"startForegroundService()" and then a "bindService()"
  • binding a Service will give you an Interface (based on that AIDL file) with which you can normally execute exposed Service's methods as they exists in the same "client" App
  • when the client App is closed don't forget to "unbindService()"

Debugging (using breakpoints from Android Studio) a Separated Process could be hard, so I suggest to do all the work in the same process and then switch to Separated only when you have all work done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM