zoukankan      html  css  js  c++  java
  • IntentService用于服务中开启子线程的自动关闭

    package com.pingyijinren.test;
    
    import android.app.IntentService;
    import android.content.Intent;
    import android.content.Context;
    import android.util.Log;
    
    /**
     * An {@link IntentService} subclass for handling asynchronous task requests in
     * a service on a separate handler thread.
     * <p/>
     * TODO: Customize class - update intent actions, extra parameters and static
     * helper methods.
     */
    public class MyIntentService extends IntentService {
        // TODO: Rename actions, choose action names that describe tasks that this
        // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
        private static final String ACTION_FOO = "com.pingyijinren.test.action.FOO";
        private static final String ACTION_BAZ = "com.pingyijinren.test.action.BAZ";
    
        // TODO: Rename parameters
        private static final String EXTRA_PARAM1 = "com.pingyijinren.test.extra.PARAM1";
        private static final String EXTRA_PARAM2 = "com.pingyijinren.test.extra.PARAM2";
    
        public MyIntentService() {
            super("MyIntentService");
        }
    
        /**
         * Starts this service to perform action Foo with the given parameters. If
         * the service is already performing a task this action will be queued.
         *
         * @see IntentService
         */
        // TODO: Customize helper method
        public static void startActionFoo(Context context, String param1, String param2) {
            Intent intent = new Intent(context, MyIntentService.class);
            intent.setAction(ACTION_FOO);
            intent.putExtra(EXTRA_PARAM1, param1);
            intent.putExtra(EXTRA_PARAM2, param2);
            context.startService(intent);
        }
    
        /**
         * Starts this service to perform action Baz with the given parameters. If
         * the service is already performing a task this action will be queued.
         *
         * @see IntentService
         */
        // TODO: Customize helper method
        public static void startActionBaz(Context context, String param1, String param2) {
            Intent intent = new Intent(context, MyIntentService.class);
            intent.setAction(ACTION_BAZ);
            intent.putExtra(EXTRA_PARAM1, param1);
            intent.putExtra(EXTRA_PARAM2, param2);
            context.startService(intent);
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            if (intent != null) {
                final String action = intent.getAction();
                if (ACTION_FOO.equals(action)) {
                    final String param1 = intent.getStringExtra(EXTRA_PARAM1);
                    final String param2 = intent.getStringExtra(EXTRA_PARAM2);
                    handleActionFoo(param1, param2);
                } else if (ACTION_BAZ.equals(action)) {
                    final String param1 = intent.getStringExtra(EXTRA_PARAM1);
                    final String param2 = intent.getStringExtra(EXTRA_PARAM2);
                    handleActionBaz(param1, param2);
                }
            }
            Log.d("MainActivity","我是子线程"+Thread.currentThread().getId()+"");
        }
    
        /**
         * Handle action Foo in the provided background thread with the provided
         * parameters.
         */
        private void handleActionFoo(String param1, String param2) {
            // TODO: Handle action Foo
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        /**
         * Handle action Baz in the provided background thread with the provided
         * parameters.
         */
        private void handleActionBaz(String param1, String param2) {
            // TODO: Handle action Baz
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        @Override
        public void onDestroy(){
            super.onDestroy();
            Log.d("MainActivity","destroy");
        }
    }
    package com.pingyijinren.test;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity{
        private Button startIntentService;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            startIntentService=(Button)findViewById(R.id.startIntentService);
    
            startIntentService.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("MainActivity","我是主线程"+Thread.currentThread().getId());
                    Intent intent=new Intent(MainActivity.this,MyIntentService.class);
                    startService(intent);
                }
            });
        }
    }
  • 相关阅读:
    CentOS6.8上Docker的安装
    IDE- VS Code-插件-Golang
    Tool-Docker-First exploration
    C++-Code-Time Transfer-Windows FILETIME(1601) To 1970 UTC
    Tool-git-command-入门笔记[慕课网-五月的夏天]
    C++-当表达式中同时存在有符号和无符号的类型时,有符号类型先转为无符号参与计算
    C语言-C语言程序设计-Practice code
    C语言-C语言程序设计-Function-strcpy
    C语言-C语言程序设计-Function-fopen
    C语言-C语言程序设计-Application-逆波兰计算器
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/5506121.html
Copyright © 2011-2022 走看看