(转)android:安卓新建服务,定时上报定位

原地址:android:安卓新建服务,定时上报定位_谢桥的博客-CSDN博客

重点
android原生系统获取的坐标是地球坐标系

这是一个服务,很简单的,
这边我稍微讲一下,
1.在启动页或者主页面申请定位权限
2.定位权限开启了,就开启服务
3.服务开启时,拿到登录状态的token,然后进入定时器,同时,定时器也在确认token是否被清空,如果被清空,就关掉定时器,同时关闭服务,这样一来,用户退出登录就不会再上传定位了
关于获取地理位置信息请看,我的另一篇文章
https://blog.csdn.net/title71/article/details/113096353

public class uploadService extends Service {
    private String uploadLocation  = "app/track/uploadLocation";
    private String Token;
    private String Address;
    private Timer timer;
    /** 标识服务如果被杀死之后的行为 */
    int mStartMode;

    /** 绑定的客户端接口 */
    IBinder mBinder;

    /** 标识是否可以使用onRebind */
    boolean mAllowRebind;
    public uploadService() {
    }

    /** 通过bindService()绑定到服务的客户端 */
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /** 当服务被创建时调用. */
    @Override
    public void onCreate() {

        //上传定位
        //拿到Token
        getToken get_token = new getToken();
        Token = get_token.getToken(uploadService.this);
        //拿到Address根地址
        mainAddress main_address = new mainAddress();
        Address = main_address.getAddress(uploadService.this);
        timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
            }
        };
        //主线程中调用:
        timer.schedule(timerTask, 3000, 3000);//延时1s,每隔500毫秒执行一次run方法
    }
    final Handler handler = new Handler() {

        //监听一下数据库有没有数据,没有数据了,就停止
        getToken get_token = new getToken();
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                String newToken = get_token.getToken(uploadService.this);
                System.out.println("newToken::::"+newToken);
                if(newToken!=null){
                    //do something
                    initLocation();
                    //开始上报
                    postTest();
                }else {
                    //关闭上传定位服务
                    timer.cancel();
                    stopService(new Intent(getBaseContext(), uploadService.class));
                }
            }
            super.handleMessage(msg);
        }
    };

    private String initLocation(){
        Location location = LocationUtils.getInstance( uploadService.this ).showLocation();
        String address = null;
        if (location != null) {
            //address = "纬度:" + location.getLatitude() + "经度:" + location.getLongitude();
            address = location.getLatitude() + "," + location.getLongitude();
            System.out.println("address::::"+address);
        }

        return address;
    }
    public void postTest(){
        OkHttpClient client = new OkHttpClient();
        //post请求
        FormBody formBody = new FormBody.Builder()
                .add("position","33")
                .build();

        Request request = new Request.Builder().url(Address+uploadLocation).
                addHeader("Token",Token).post(formBody).build();

        client.newCall(request).enqueue(new Callback() {
            public void onFailure(Call call, IOException e) {
                System.out.println(e.getMessage());
            }

            public void onResponse(Call call, Response response) throws IOException {
                if(response.code() >= 200 && response.code() < 300) {
                    String result = response.body().string();
                    System.out.println("uploadService::::"+result);
                }
            }
        });
    }

    /** 调用startService()启动服务时回调 */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return mStartMode;
    }

    /** 通过unbindService()解除所有客户端绑定时调用 */
    @Override
    public boolean onUnbind(Intent intent) {
        return mAllowRebind;
    }

    /** 通过bindService()将客户端绑定到服务时调用*/
    @Override
    public void onRebind(Intent intent) {

    }

    /** 服务不再有用且将要被销毁时调用 */
    @Override
    public void onDestroy() {

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值