10 IntentService 启动单独的新线程

本文通过MainActivity启动两种服务:MyService和MyIntentService,并对比了这两种服务的执行方式。MyService直接在主线程中运行耗时任务,可能导致ANR;而MyIntentService则在独立线程中执行耗时任务。

--------------------------------------main.java------------------------

package com.example.nh;


import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


public void getStartServiceOnClick(View view) {
//创建需要启动的Service的Intent
Intent intent = new Intent(this, MyService.class);
//启动Service
startService(intent);
}
public void getStartIntentServiceOnClick(View view) {
//创建需要启动的IntentService的Intent
Intent intent = new Intent(this, MyIntentService.class);
//启动IntentService
startService(intent);
}
}

------------------------------------MyService.java----------------

package com.example.nh;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;


public class MyService extends Service {


@Override
public IBinder onBind(Intent intent) {
return null;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//该方法执行耗时任务可能导致ANR(Application Not Responding)异常
long endTime = System.currentTimeMillis() + 20 * 1000;
System.out.println("onstart");
while(System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("-------执行耗时任务完成");
return Service.START_NOT_STICKY;
}



}

---------------------------------MyIntentService.java----------------------

package com.example.nh;


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


public class MyIntentService extends IntentService {


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


//IntentService 会使用单独的线程来执行该方法的代码
@Override
protected void onHandleIntent(Intent intent) {
//该方法内可以执行耗时任务,比如下载文件等,此处只是让线程暂停20秒
long endTime = System.currentTimeMillis() + 20 * 1000;
System.out.println("onStart");
while(System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("----耗时任务执行完成。。。。。");
}


}


。。。。。。。。。。。。。。main.xml。。。。。。。。。。。。。。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <Button
        android:onClick="getStartServiceOnClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="startService" />
    <Button
        android:onClick="getStartIntentServiceOnClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="startIntentService" />


</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值