简体   繁体   English

如何创建可从android中的并发应用程序访问功能的后台服务?

[英]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. 但是,此服务将需要在后台运行并维护SQLiteConnection对象,而无需关闭/重新启动。

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. 我需要访问服务中的函数,这些函数将使用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. 我尝试创建和启动服务,但是当我切换活动时,Service对象被破坏。 I've tried using other methods of maintaining an SQLiteConnection object to no avail such as the following code. 我尝试使用其他维护SQLiteConnection对象的方法无济于事,例如以下代码。

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). 我正确能够使用SQLiteConnection对象的唯一方法是在一个活动中进行所有操作(插入,选择,更新,删除)。 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. 因此,从那以后,我开始研究运行包含以上所有查询功能的并发应用程序(永远不会关闭),然后客户端应用程序将调用这些功能,甚至不需要查看SQLiteConnection对象。 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) 我已经看过并尝试过的所有示例似乎都不在我的应用程序之外运行(我无法通过android设置在“运行服务”中看到它们)

How to call methods of a Service from activity? 如何从活动中调用服务的方法?

https://github.com/MysticMagic/ServiceAndSqlite 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; 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. 我正在使用一个名为litesync的库,我所需要的只是一个SQliteConnection对象,该对象由持续运行的服务维护。 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) 这里的主要目标是使用相同的SQLiteConnection对象(该对象保存服务器数据库的地址)

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. 因此,我正在寻找一个单一的连续后台服务,该服务将处理对SQLiteConnection对象的所有调用,该对象永远不会关闭并且在一个类中起作用。

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. 您必须创建一个驻留在单独流程中的IntentService(具有自己的线程)(我认为此步骤可以是可选的,但我建议让它驻留在其自己的流程中),然后使用“ AIDL进程间-通信技术”,以在活动/应用程序和此服务之间进行通信。 It's impossible to write all the code here, but I'll give you few hints: 这里不可能编写所有代码,但我会给您一些提示:

  • create the IntentService 创建IntentService
  • create an AIDL file that expones all methods you will want to use from this Service 创建一个AIDL文件,该文件将列出您希望通过此服务使用的所有方法
  • starting from Android 6 all "permanent" Services should display a notification while running, so you have to make that Service in foreground 从Android 6开始,所有“永久”服务均应在运行时显示通知,因此您必须将该服务置于前台
  • do a "startService()"/"startForegroundService()" and then a "bindService()" 做一个“ startService()” /“ startForegroundService()”,然后一个“ 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 绑定服务将为您提供一个接口(基于该AIDL文件),您可以使用该接口正常执行公开的服务方法,因为它们存在于同一“客户端”应用程序中
  • when the client App is closed don't forget to "unbindService()" 当客户端应用关闭时,不要忘记“ 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. 调试(使用Android Studio中的断点)一个单独的过程可能很困难,因此我建议在同一过程中完成所有工作,然后仅在完成所有工作后才切换到单独。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在android中创建后台服务? - How I can create a Background service in android? 如何从后台服务更新Android活动中的信息 - How can I update information in an Android Activity from a background Service 我如何从后台服务向Android中的活动发送列表 - How can i send a list from Background service to activity in android 如何从 Android 服务获取应用程序上下文? - How can I get the application context from an Android Service? 如何从laravel Web应用程序创建Android应用程序? - How can I create a android app from laravel web application? Android:如何在不调用 onCreate 方法的情况下从服务后台恢复应用程序? - Android: How can I resume an app from background from service without calling onCreate method? 如何在ionic 2应用程序中创建后台服务? - How to create a background service in ionic 2 application? 如何从房间数据库或android后台服务中的活动意图中一致地获取数据 - How can I took data consistently whether from room database or activity intent in android background service 如何创建多语言Android应用程序? - How can I create a multilingual android application? 如何防止Android中的后台服务被强制停止? - How can I prevent a background Service in Android from being force stopped?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM