zoukankan      html  css  js  c++  java
  • 更加省心的服务,IntentService的使用

    通过前两篇文章的学习,我们知道了服务的代码是默认运行在主线程里的,因此,如果要在服务里面执行耗时操作的代码,我们就需要开启一个子线程去处理这些代码。比如我们可以在 onStartCommand方法里面开启子线程来处理耗时代码。

        public int onStartCommand(Intent intent, int flags, int startId) {
            
            Thread thread = new Thread(){
                @Override
                public void run() {
                    
                    /**
                     * 耗时的代码在子线程里面写
                     */
                    
                }
            };
            thread.start();
            
            return super.onStartCommand(intent, flags, startId);
        }

    但是,我们都知道,服务一旦启动,就会一直运行下去,必须调用stopService()或者stopSelf()方法才能让服务停止下来。所以,我们来修改一下run方法

     public void run() {
                    
                    /**
                     * 耗时的代码在子线程里面写
                     */
                    stopSelf();
                    
                }

    就这样,我们很容易的就在服务里面开启了一个线程,然后在代码最后面加上stopSelf();这样就可以在代码运行结束的时候,结束服务了。但是这样对于我们开发者来说,是不是有些麻烦呢,确实有点麻烦,比如你有时候忘记了开启线程呢?或者忘记了调用stopSelf()?所以,谷歌给我们一个很好的类,通过这个类我们就可以不用管这些东西,因为这个类已经帮我们实现了在子线程中操作代码了。同时,但子线程代码执行完毕,这个服务会自动销毁,不用再占用内存资源。所以,我们通过这个类,就可以不用去开启线程,也不用去销毁这个服务。因为,这个类都帮我们处理好了。这个类就是IntentService。这个类的使用和Service大同小异,但是比Service更加省心,更加方便。下面我们直接看代码吧,我习惯在代码中讲解。

    布局文件就是一个按钮而已,先看布局。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        
        
        <Button 
            android:id="@+id/bt_start" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
        
    
    </LinearLayout>
    intentservice.xml

    和Service一样,使用的话,要在清单文件中

     <service android:name="com.example.mydemo.intentservice.MyIntentService"></service>
    View Code

    然后是服务类

    package com.example.mydemo.intentservice;
    
    import android.annotation.SuppressLint;
    import android.app.IntentService;
    import android.content.Intent;
    import android.os.Handler;
    import android.os.Message;
    import android.widget.Toast;
    
    /**
     *************************************************************** 
     * 
     * @版权 LinFeng
     * 
     * @作者 LinFeng
     * 
     * @版本 1.0
     * 
     * @创建日期 2016-6-10
     * 
     * @功能描述 
     ***************************************************************** 
     */
    
    /**
     * 注意,继承的是 IntentService而不是Service哦,还有,既然是服务,那么就必须在清单文件里面注册
     */
    public class MyIntentService extends IntentService {
        
        
        
        @SuppressLint("HandlerLeak") 
        Handler handler = new Handler(){
            
            @Override
            public void handleMessage(Message msg) {
                Toast.makeText(MyIntentService.this, "IntetnService Running", Toast.LENGTH_SHORT)
                .show();
                super.handleMessage(msg);
                
            }
        };
    
        /**
         * 使用Eclipse如果没有添加这个无参构造函数的话会报一个运行时错误: java.lang.InstantiationException
         */
        public MyIntentService() {
            /**
             * 这里只需要传入一个字符串就可以了
             */
            super("MyIntentService");
    
        }
    
        /**
         * 必须实现的抽象方法,我们的业务逻辑就是在这个方法里面去实现的 在这个方法里实现业务逻辑,我们就不用去关心ANR的问题
         */
        @Override
        protected void onHandleIntent(Intent intent) {
            
            /**
             * 因为这个方法是在子线程里面处理的,所以这里我们不能直接在子线程里面弹Toast
             * 我们这里使用handler来帮助我们处理Toast
             */
            handler.sendEmptyMessage(0);
        }
    
        /**
         * 为了验证onHandleIntent执行后,服务会不会自动销毁,我们在这里重写onDestroy方法
         * 如果会自动销毁,那么在"IntetnService Running"出现后,应该会出现"IntetnService Stop"
         */
        @Override
        public void onDestroy() {
            super.onDestroy();
            Toast.makeText(this, "IntetnService Stop", Toast.LENGTH_SHORT).show();
        }
    
    }
    MyIntentService

    然后就是主界面类,主界面类也没干啥事情,就是按钮点击事件,点击后启动服务,启动IntentService和启动普通的Service没有差别,都是通过意图来启动的。

    package com.example.mydemo.intentservice;
    
    import com.example.mydemo.R;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    /**
     *************************************************************** 
     * 
     * @版权 LinFeng
     * 
     * @作者 LinFeng
     * 
     * @版本 1.0
     * 
     * @创建日期 2016-6-10
     * 
     * @功能描述 
     ***************************************************************** 
     */
    public class IntentServiceActivity extends Activity{
        
        private Button btButton;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.intentservice);
            
            btButton = (Button) findViewById(R.id.bt_start);
            btButton.setText("Start IntentService");
            
            btButton.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(IntentServiceActivity.this,MyIntentService.class);
                    startService(intent);
                }
            });
        }
        
    
    }
    IntentServiceActivity

    然后就是运行截图了

  • 相关阅读:
    atitit.ntfs ext 文件系统新特性对比
    Atitit.图片木马的原理与防范 attilax 总结
    Atitit.图片木马的原理与防范 attilax 总结
    Atitit.jdk java8的语法特性详解 attilax 总结
    Atitit.jdk java8的语法特性详解 attilax 总结
    Atitit.远程接口 监控与木马   常用的api 标准化v2 q216
    Atitit.远程接口 监控与木马   常用的api 标准化v2 q216
    Atitit..jdk java 各版本新特性 1.0 1.1 1.2 1.3 1.4 1.5(5.0) 1.6(6.0) 7.0 8.0 9.0 attilax 大总结
    Atitit..jdk java 各版本新特性 1.0 1.1 1.2 1.3 1.4 1.5(5.0) 1.6(6.0) 7.0 8.0 9.0 attilax 大总结
    Atitit.跨平台预定义函数 魔术方法 魔术函数 钩子函数 api兼容性草案 v2 q216  java c# php js.docx
  • 原文地址:https://www.cnblogs.com/linfenghp/p/5574491.html
Copyright © 2011-2022 走看看