5.2(小组冲刺第七天)

进行计步器编写今日完成代码量
package com.example.mapdemo;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mapdemo.jibu.BindService;
import com.example.mapdemo.jibu.UpdateUiCallBack;

public class JibuActivity extends AppCompatActivity {
private BindService bindService;
private TextView textView;
private boolean isBind;
//动态申请健康运动权限
private String[] permissions={Manifest.permission.ACTIVITY_RECOGNITION};
private AlertDialog dialog;

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (msg.what == 1) {
            textView.setText(msg.arg1 + "");
        }
    }
};

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jibu);
    textView = (TextView) findViewById(R.id.busu);
    Intent intent = new Intent(JibuActivity.this, BindService.class);
    isBind = bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    startService(intent);
    // 版本判断。当手机系统是安卓10.0时,才有必要去判断权限是否获取
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        // 检查该权限是否已经获取
        int get = ContextCompat.checkSelfPermission(this, permissions[0]);
        // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
        if (get != PackageManager.PERMISSION_GRANTED) {
            // 如果没有授予该权限,就去提示用户请求自动开启权限
            ActivityCompat.requestPermissions(this, permissions, 321);
        } else {//如果已经申请,则进入应用(这里填onCreate方法后面的代码)
            intent = new Intent(JibuActivity.this, BindService.class);
            isBind =  bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
            startService(intent);
        }
    } else {//如果版本低于10.0,则直接进入应用(这里填onCreate方法后面的代码)
        intent = new Intent(JibuActivity.this, BindService.class);
        isBind =  bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        startService(intent);
    }


}
@RequiresApi(api = Build.VERSION_CODES.M)
protected void m( View view){
    startActivity(new Intent(getApplicationContext(), MainActivity.class ));
}
//和绷定服务数据交换的桥梁,可以通过IBinder service获取服务的实例来调用服务的方法或者数据
private ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        BindService.LcBinder lcBinder = (BindService.LcBinder) service;
        bindService = lcBinder.getService();
        bindService.registerCallback(new UpdateUiCallBack() {
            @Override
            public void updateUi(int stepCount) {
                //当前接收到stepCount数据,就是最新的步数
                Message message = Message.obtain();
                message.what = 1;
                message.arg1 = stepCount;
                handler.sendMessage(message);
                Log.i("MainActivity—updateUi","当前步数"+stepCount);
            }
        });
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
};
posted @ 2024-06-10 22:24  孤儿组  阅读(15)  评论(0)    收藏  举报