【问题标题】:about background services关于后台服务
【发布时间】:2018-10-06 17:59:43
【问题描述】:

我正在为 Android 开发一个应用程序,它应该通过类似于 MQTT 的协议从 Arduino 设备接收一些通知。此通知将是一种警报,不会持续,但很少见。该服务将完成一项简单的工作:接收单个通知,它在 sqlite 数据库中注册并向系统栏发送通知。对于 API 级别 26 或更高级别,我应该使用哪种后台服务。我知道系统对运行后台服务施加了限制 谢谢 路易吉

【问题讨论】:

    标签: android mqtt iot


    【解决方案1】:
    /*Note: IntentService is a service, and is therefore subject to the new restrictions on background services. As a result, many apps that rely on IntentService do not work properly when targeting Android 8.0 or higher. For this reason, Android Support Library 26.0.0 introduces a new JobIntentService class, which provides the same functionality as IntentService but uses jobs instead of services when running on Android 8.0 or higher.*/
    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Handler;
    import android.os.SystemClock;
    import android.support.v4.app.JobIntentService;
    import android.util.Log;
    import android.widget.Toast;
    
    /**
     * Example implementation of a JobIntentService.
     */
    public class SimpleJobIntentService extends JobIntentService {
        /**
         * Unique job ID for this service.
         */
        static final int JOB_ID = 1000;
    
        /**
         * Convenience method for enqueuing work in to this service.
         */
        static void enqueueWork(Context context, Intent work) {
            enqueueWork(context, SimpleJobIntentService.class, JOB_ID, work);
        }
    
        @Override
        protected void onHandleWork(Intent intent) {
            // We have received work to do.  The system or framework is already
            // holding a wake lock for us at this point, so we can just go.
            Log.i("SimpleJobIntentService", "Executing work: " + intent);
            String label = intent.getStringExtra("label");
            if (label == null) {
                label = intent.toString();
            }
            toast("Executing: " + label);
            for (int i = 0; i < 5; i++) {
                Log.i("SimpleJobIntentService", "Running service " + (i + 1)
                        + "/5 @ " + SystemClock.elapsedRealtime());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
            Log.i("SimpleJobIntentService", "Completed service @ " + SystemClock.elapsedRealtime());
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            toast("All work complete");
        }
    
        final Handler mHandler = new Handler();
    
        // Helper for showing tests
        void toast(final CharSequence text) {
            mHandler.post(new Runnable() {
                @Override public void run() {
                    Toast.makeText(SimpleJobIntentService.this, text, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    

    【讨论】:

    • 这不能回答问题
    猜你喜欢
    • 2020-09-22
    • 2020-07-07
    • 2018-06-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2021-01-03
    • 1970-01-01
    相关资源
    最近更新 更多