Android 四大组件之一:Service后台服务之IntentService

一、IntentService与Service的区别

  感觉说是IntentService与Service的区别也可以理解为我们为什么使用IntentService,在我们是用service的时候之前我们有说过耗时操作可以在它里面进行但是需要新开启一个线程去进行耗时操作,但是这种服务在运行的之后不能自动停止,需要我们在应用程序中找到正在运行的服务中找到它手动关闭。在这里google为我们提供了一种可以自动回收的服务即IntentService。
  IntentService与Service相比①Service 是运行在主线程中服务而IntentService是自动又重新开启了一个线程。②Service在第一次启动后会执行onCreate与Onstartcommon的方法,再次点击开始按钮只会执行Onstartcommon方法,界面不可动;而IntentService中点击开始按钮会有一个消息队列的机制,每次点击启动服务时,该操作会被加入到消息队列中排队等待,等前面的任务完成了再执行后面的,每次都会运行 onHandleIntent()方法,界面可动。③Service服务是被销毁而IntentService不会被销毁而是被系统回收,因此它不调用onDestroy方法。

二、使用实例

1、写一个class继承IntentService,这里要注意在类中我们必须写一个无参构造器,不然会报错。重写onHandleIntent方法。

package com.example.myservice;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class MyIntentService extends IntentService{

    public MyIntentService(String name) {
        super(name);

    }
    public MyIntentService() {
        this("");

    }
    @Override
    protected void onHandleIntent(Intent intent) {
    //开启新的线程改变progressbar的进度。
    new Thread(new Runnable() {

            @Override
            public void run() {
                int count=0;
                while(true){
                    if(count==100){             
            // Toast.makeText(getApplicationContext(), "已完成下载", Toast.LENGTH_LONG).show();
                    Log.d("输出", "已完成下载");
                    count=101;
                    }if(count<100){
                    count++;
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Intent intent=new Intent();
                    intent.setAction(MainActivity.DOWNLOAD);
                    intent.putExtra("count", count);
                    sendBroadcast(intent);
                }
            }
            }
        }).start();

    }

}
  • 1

2、在manifest中注册

 <service android:name=".MyIntentService"></service>
  • 1

3、MainActivity

package com.example.myservice;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity implements OnClickListener {
    private Button mbtn_start;
    private Button mbtn_stop;
    private Button mbtn_download;
    private ProgressBar mprogressbar;
    private Myreceivce myrecevice;
    public static final String DOWNLOAD="com.receiver.test";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mbtn_start = (Button) findViewById(R.id.start);
        mbtn_stop = (Button) findViewById(R.id.stop);

        mbtn_download=(Button) findViewById(R.id.bt_download);
        mprogressbar=(ProgressBar) findViewById(R.id.progressbar);

        mbtn_start.setOnClickListener(this);
        mbtn_stop.setOnClickListener(this);
        mbtn_download.setOnClickListener(this);

        IntentFilter filter=new IntentFilter(DOWNLOAD);
        myrecevice=new Myreceivce();
        registerReceiver(myrecevice, filter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.start:
            Intent intent=new Intent(getApplicationContext(),MyServices.class);
            startService(intent);
            break;
        case R.id.stop:
            Intent intent1=new Intent(getApplicationContext(),MyServices.class);
            stopService(intent1);
            break;
            case R.id.bt_download:
                Intent intent2=new Intent(getApplicationContext(),MyIntentService.class);
                    startService(intent2);
                break;
        default:
            break;
        }

    }
    class Myreceivce extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            int  count=intent.getIntExtra("count",0);
            mprogressbar.setProgress(count);
        }

    }
    @Override
    protected void onDestroy() {

        super.onDestroy();
        unregisterReceiver(myrecevice);
        Log.d("输出", "进程结束");
    }
}
  • 1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是原来的你吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值