Service实现后台动态更新数据到UI

具体请看代码:注释掉的就是没有用到的代码,业务逻辑可以按照文字注释去编辑

package main.com.pwj.testmvpv1;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.Context;
import android.os.Binder;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;
//数据采集服务,开启后可以自动件数据保存到数据库,同时提供更新界面的接口
/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p>
 * TODO: Customize class - update intent actions, extra parameters and static
 * helper methods.
 */
public class SampleService extends IntentService {

    public SampleBinder sampleBinder=new SampleBinder();
    public int test_i=0;
    private boolean actionBoolean=false;
    public class  SampleBinder extends Binder{
    }

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

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

    @Override
    public void onCreate() {
        super.onCreate();
        //接入蓝牙数据,异步监听
          if (actionBoolean==false){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // Log.i("SampleService","Runnable");
                    while (true){
                        //  Log.i("SampleService","RunnableValue"+test_i);
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        test_i++;
                    }

                }
            }).start();
            actionBoolean=true;
        }
        Log.i("SampleService","onCreate1");
        test_i++;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        //线程内处理逻辑,存储数据到数据库
        //回调接口更新UI界面
        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                Log.i("SampleService","onHandleIntent"+test_i);

                
            }
        }, 0, 10);
    }


    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {


//
//        AlarmManager manager=(AlarmManager)getSystemService(ALARM_SERVICE);
//        int anHour=1000;
//        long triggerAtTime= SystemClock.elapsedRealtime()+anHour;
//        Intent i=new Intent(this,SampleService.class);
//        PendingIntent pendingIntent=PendingIntent.getService(this,0,i,0);
//        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pendingIntent);
        Log.i("SampleService","onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鼾声鼾语

感谢您的支持鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值