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);    }}
原创粉丝点击