Android IntentService 可执行耗时任务的Service
来源:互联网 发布:怎么联系淘宝卖家 编辑:程序博客网 时间:2024/05/30 04:16
<service android:name=".MyService" /> <service android:name=".MyIntentService" />
MyService.java
package shortcut.song.com.myapplication;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.support.annotation.IntDef;import android.support.annotation.Nullable;/** * Created by Administrator on 2017/8/14 0014. */public class MyService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); } @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 (Exception e) { e.printStackTrace(); } } } System.out.println("----耗时任务完成----"); return START_STICKY; }}
MyIntentService.java
package shortcut.song.com.myapplication;import android.app.IntentService;import android.content.Intent;import android.support.annotation.Nullable;/** * Created by Administrator on 2017/8/14 0014. */public class MyIntentService extends IntentService { public MyIntentService (){ super("MyIntentService"); } // IntentService 会使用单独的线程来执行该方法的代码 @Override protected void onHandleIntent(@Nullable Intent intent) { // 该方法内可以执行任何耗时任务,如下载文件等 , long endTime = System.currentTimeMillis() + 20 * 1000; System.out.println("onHandleIntent"); while (System.currentTimeMillis() < endTime) { synchronized (this) { try { wait(endTime - System.currentTimeMillis()); }catch (Exception e) { e.printStackTrace(); } } } System.out.println("----耗时任务执行完成----"); }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="shortcut.song.com.myapplication.IntentServiceActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Service" android:onClick="startService"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="startIntentService" android:onClick="startIntentService"/></LinearLayout>
package shortcut.song.com.myapplication;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class IntentServiceActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intent_service); } public void startService(View v){ // 创建需要启动的Service的Intent Intent intent = new Intent(IntentServiceActivity.this, MyService.class); startService(intent); } public void startIntentService(View v) { // 创建需要启动的IntentService的Intent Intent intent = new Intent(IntentServiceActivity.this, MyIntentService.class); // 启动IntentService startService(intent); }}
阅读全文
0 0
- Android IntentService 可执行耗时任务的Service
- Android使用IntentService执行耗时任务
- Service个人笔记4 --IntentService处理耗时任务
- 使用IntentService在Service中创建耗时任务
- 利用IntentService实现在broadcastreceiver中比较耗时的任务
- Android的后台任务IntentService
- IntentService执行耗时任务及源码解析
- Android Service的总结,service, bindservice,intentservice
- Android之Service与IntentService的比较
- Android之Service与IntentService的比较
- Android之Service与IntentService的比较
- Android之Service与IntentService的比较
- Android之Service与IntentService的比较
- Android中IntentService与Service的区别
- Android之Service与IntentService的比较
- Android之Service与IntentService的比较
- Android之Service与IntentService的比较
- Android之Service与IntentService的比较
- 深入理解java泛型
- 详细图解如何注册 Navicat for Mysql 11.0.17 企业版(Win7 64bit)
- Adding new nodes to GI
- volatile和Cache一致性协议之MESI
- Ubuntu 17.04编译串口发送16进制数据包报了个神奇的错误—续
- Android IntentService 可执行耗时任务的Service
- HDU2034 人见人爱A-B
- 重要的Attach
- Fork/Join框架的简单学习和简单demo
- php文件下载
- 完美的代价
- 安装node.js之后的操作
- phpstorm:如何设置代码自动换行
- java集合之HashMap的扩容resize