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>
- 10 IntentService 启动单独的新线程
- android线程中启动新的activity
- Java启动新线程的额外开销
- android启动新线程的方法
- 怎样sleep单独的线程
- Service学习——第五篇 使用IntentService在单独线程上执行异步任务
- IntentService通过HandlerThread单独开启一个线程来处理所有Intent请求对象
- 单独启动Xserver的步骤
- 新线程启动窗口
- 启动新线程
- Android 的线程(AsyncTask、HandlerThread、IntentService详解)和线程池
- Android 的线程(AsyncTask、IntentService详解)和线程池
- Android 的线程(AsyncTask、HandlerThread、IntentService详解)和线程池
- 单独设置Tomcat的JDK启动环境
- 单独启动Microsoft Help Viewer的方法
- Android Handler消息机制:没有启动新的线程
- VC启动一个新线程的三种方法
- 静态初始化块里启动新线程的陷阱
- 有关search及ftsearch的一点总结
- linux进程管理(C语言)
- 驱动器相关的操作
- URAL 1893. A380 (阅读理解 + 模拟)
- Android Camera 明细(一)
- 10 IntentService 启动单独的新线程
- 怎样在TI stellaris lm3sxxx 程序中使用C语言printf()函数进行串口输出
- 【设计模式】原型模式
- 代理模式(Python版)---为别人做嫁衣
- 运筹帷幄之中,决胜千里之外——运筹学1-3章
- 算法练习系列—hiho1044 状态压缩二(捡垃圾)
- c_基础_获取main函数返回值
- 1084. Broken Keyboard (20)
- 简单位操作