【问题标题】:Clarification on Android foreground service关于Android前台服务的说明
【发布时间】:2017-12-15 01:00:58
【问题描述】:

我的应用目前使用后台服务与物理设备进行通信(蓝牙)。 (我制造和销售物理设备。)应用程序每 500 毫秒向设备发送一个命令。用户启动这个过程,它必须一直运行,直到用户停止它。当应用程序处于活动状态时,该过程的结果将发送到应用程序。即使应用程序未处于活动状态,此过程也需要运行。 (即他们接听电话、搜索网络。)一旦应用再次激活,进程的状态就会与应用同步。该过程可以在任何地方运行几分钟到几个小时。 (是的,如果用户想要运行该过程 99 小时,则需要插入。)大多数用户运行它 2-15 分钟。现在一切都很好,但是使用 API 26,看起来这种架构不再被允许。一种迁移选项是移动到前台服务。但是,我发现文档不清楚前台服务是如何工作的。前台服务是否在应用未激活的情况下继续运行? (即它已经通过了 onPause。)如果是这样,这与后台服务有何不同?是否有更好的关于前台服务如何工作的文档。 (我的网络搜索没有发现任何重要的东西。)Alos,如果新的限制仍然适用,API 26 文档没有说明应用程序是否绑定到后台服务。是吗?

谢谢, 斯坦

【问题讨论】:

    标签: android


    【解决方案1】:

    前台服务是您置于前台状态的服务,这意味着如果进程需要 CPU 或您的应用已关闭,系统不会终止该进程。

    首先你有 3 种服务:

    • 已启动的服务(在 UI 线程中运行)
    • IntentService(在自己的线程中运行)(参见Services vs IntentServices
    • 绑定服务(只要有一个活动活动绑定它就运行)

    如上所述,如果你关闭你的应用程序,一个绑定服务也会被关闭,它是由bindService()启动的。

    IntentServicesService 的子类型,它简化了传入意图的“工作队列过程”,即它在队列中一个接一个地处理传入意图,如@ 987654323@。它有一个默认实现,由startService() 启动。主要用于异步任务。

    已启动的服务是由组件启动的服务,并继续存在直到调用 stopService() 或您的应用关闭。

    使用前台服务使您的Service持久。你必须在你的服务中调用startForeground()。它仍然会运行,直到您停止 Service,例如使用 stopSelf()stopService()

    注意每次调用startService()都会触发onStartCommand(),但onCreate()只会触发一次。

    这是一个前台启动服务的简单实现

    在您的 Manifest.xml 中:

    <service android:name=".ConnectionService"
        android:enabled="true"/>
    

    在 MyService.java 中:

    public class MyService extends Service {
        // Unique notification identifier
        private final static int NOTIFICATION_ID = 95;
    
        private NotificationManager mNotificationManager;
    
        public MyService() { super(); }
    
        @Override
        public void onCreate() {
            // Initialize notification
            mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    
            // Build your notification here
            mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
            mBuilder.setSmallIcon(R.mipmap.ic_small_icon);
            mBuilder.setContentTitle("MyService");
            mBuilder.setContentText("The Service is currently running");
    
            // Launch notification
            startForeground(NOTIFICATION_ID, mBuilder.build());
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // Handle startService() if you need to
            // for exmple if you are passing data in your intent
            return START_NOT_STICKY;
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // We don't provide binding, so return null
            return null;
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            // Remove the notification when the service is stopped
            mNotificationManager.cancel(NOTIFICATION_ID);
        }
    }
    

    最后只需致电startService()

    【讨论】:

      【解决方案2】:

      前台服务对我来说是新的,但它看起来像一个在状态栏中有一个图标的服务,所以它确实有一个用户界面。你查过jobinfo吗?似乎是在后台运行作业的一种方式。如果您希望您的服务在启动完成时启动this article may help 最后,这可能是一个很长的镜头,在 android 设备上转到设置>应用程序并选择一个已安装的应用程序。你会发现一个屏幕弹出,因为程序员覆盖了 android.accounts.AccountAuthenticator 操作。换句话说,系统通过 Launcher Intent 以外的方式启动了一个活动。以同样的方式,系统必须调用一个意图,您可以在蓝牙连接时覆盖 android.bluetooth.device.action.FOUND check this out

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-22
        • 1970-01-01
        • 2011-11-16
        相关资源
        最近更新 更多