码迷,mamicode.com
首页 > 其他好文 > 详细

IntentService的用法,对比Service它会按顺序执行,不会像Service一样并发执行。

时间:2015-11-09 10:34:46      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

package com.lixu.intentservice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for(int i=0;i<20;i++){    
            Intent intent=new Intent(this,MyAppService.class);
            intent.putExtra(Changliang.KEY, i+"");
            
            startService(intent);
        }
    }
 //不要忘了关闭服务
    @Override
    protected void onDestroy() {
        Intent intent=new Intent(this,MyAppService.class);
        stopService(intent);
        super.onDestroy();
    }


}
package com.lixu.intentservice;

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

public class MyAppService extends IntentService{
    //构造方法要修改
    public MyAppService() {
        super("lixu");
    }

    
    @Override
    protected void onHandleIntent(Intent intent) {
        
        String str=intent.getStringExtra(Changliang.KEY);
        Log.e("MyAppService","内容"+ str);
        int content=0;
        final int A=content++;
        Log.e("MyAppService","线程"+ A+"开始执行");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.e("MyAppService", "线程"+ A+"结束");
        
    }

}

 

IntentService的用法,对比Service它会按顺序执行,不会像Service一样并发执行。

标签:

原文地址:http://www.cnblogs.com/labixiaoxin/p/4949197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!