10 IntentService 启动单独的新线程

来源:互联网 发布:英语面试技巧知乎 编辑:程序博客网 时间:2024/05/04 13:05

--------------------------------------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>

0 0
原创粉丝点击