我需要一些指导,如何使用一个简单的应用程序,显示和节省速度。
现在我有一个带有主活动和LocationService的应用程序。它保存的很好,但我找不到一种方法来不断更新UI。我已经在mainActivity中制作了其他带有位置代码的应用程序,但我希望在屏幕关闭时继续传输,所以我尝试了一个服务,但看起来我必须在mainActivity中为UI实现位置提供程序?
IM是否缺少某项功能?
我如何使用这个应用程序?
这是我的最新版本,我不能发送速度回用户界面。
位置服务
public class LocationService extends Service {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference(Build.MANUFACTURER+" "+Build.DEVICE);
DatabaseReference myLiveRef = myRef.child("LiveSpeed");
DatabaseReference myPastsRef = myRef.child("PastSpeeds");
FusedLocationProviderClient fusedLocationClient;
LocationRequest locationRequest;
LocationCallback locationCallback;
private float speed;
private final IBinder mBinder = new MyBinder();
class MyBinder extends Binder {
public LocationService getService(){
return LocationService.this;
}
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
stopSelf();
}
@Override
public IBinder onBind(Intent intent) {
Log.d("TAGZ","onBInd");
return mBinder;
}
private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());
}
@Override
public void onCreate() {
super.onCreate();
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) createNotificationChanel() ;
else startForeground(
1,
new Notification()
);
locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 1000)
.setWaitForAccurateLocation(false)
.setMinUpdateIntervalMillis(500)
.setMaxUpdateDelayMillis(1500)
.build();
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(@NonNull LocationResult locationResult) {
Location location = locationResult.getLastLocation();
speed = location.getSpeed()*3.6f;
myLiveRef.setValue(speed);
DatabaseReference newPastRef = myPastsRef.push();
newPastRef.setValue(String.valueOf(Calendar.getInstance().getTime())+" |||||||| "+ speed +" KM/H");
}
};
startLocationUpdates();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
fusedLocationClient.removeLocationUpdates(locationCallback);
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChanel() {
String notificationChannelId = "Location channel id";
String channelName = "Background Service";
NotificationChannel chan = new NotificationChannel(
notificationChannelId,
channelName,
NotificationManager.IMPORTANCE_NONE
);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, notificationChannelId);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("Location updates:")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
}
主要活动
public class MainActivity extends AppCompatActivity {
private static final int MY_FINE_LOCATION_REQUEST = 99;
private static final int MY_BACKGROUND_LOCATION_REQUEST = 100;
TextView textView;
Intent mServiceIntent;
Button startServiceBtn, stopServiceBtn;
private LocationService mLocationService;
private boolean mBound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startServiceBtn = findViewById(R.id.start_service_btn);
stopServiceBtn = findViewById(R.id.stop_service_btn);
textView = findViewById(R.id.textView);
startServiceBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Background permission");
alertDialog.setMessage(getString(R.string.background_location_permission_message));
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Start service anyway",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
starServiceFunc();
dialog.dismiss();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Grant background Permission",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
requestBackgroundLocationPermission();
dialog.dismiss();
}
});
alertDialog.show();
}else if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
== PackageManager.PERMISSION_GRANTED){
starServiceFunc();
}
}
else{
starServiceFunc();
}
}else if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("ACCESS_FINE_LOCATION");
alertDialog.setMessage("Location permission required");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
requestFineLocationPermission();
dialog.dismiss();
}
});
alertDialog.show();
} else {
requestFineLocationPermission();
}
}
}
});
stopServiceBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
stopServiceFunc();
}
});
}
@Override
protected void onResume() {
super.onResume();
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
LocationService.MyBinder binder = (LocationService.MyBinder) service;
mLocationService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
}
};
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Toast.makeText(this, Integer.toString(requestCode), Toast.LENGTH_LONG).show();
if ( requestCode == MY_FINE_LOCATION_REQUEST){
if (grantResults.length !=0 /*grantResults.isNotEmpty()*/ && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
requestBackgroundLocationPermission();
}
} else {
Toast.makeText(this, "ACCESS_FINE_LOCATION permission denied", Toast.LENGTH_LONG).show();
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("lauchmodesdemo.youtube.codetutor.com.speedtest")
));
}
}
}else if (requestCode == MY_BACKGROUND_LOCATION_REQUEST){
if (grantResults.length!=0 /*grantResults.isNotEmpty()*/ && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Background location Permission Granted", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "Background location permission denied", Toast.LENGTH_LONG).show();
}
}
}
private void starServiceFunc(){
mLocationService = new LocationService();
mServiceIntent = new Intent(this, mLocationService.getClass());
if (!Util.isMyServiceRunning(mLocationService.getClass(), this)) {
startService(mServiceIntent);
bindService(mServiceIntent,connection, Context.BIND_AUTO_CREATE);
Toast.makeText(this, getString(R.string.service_start_successfully), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, getString(R.string.service_already_running), Toast.LENGTH_SHORT).show();
}
}
private void stopServiceFunc(){
if (Util.isMyServiceRunning(mLocationService.getClass(), this)) {
stopService(mServiceIntent);
Toast.makeText(this, "Service stopped!!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Service is already stopped!!", Toast.LENGTH_SHORT).show();
}
}
private void requestBackgroundLocationPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},
MY_BACKGROUND_LOCATION_REQUEST);
}
}
private void requestFineLocationPermission() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, MY_FINE_LOCATION_REQUEST);
}
}
实用程序
public class Util {
public static boolean isMyServiceRunning(Class<?> serviceClass, Activity mActivity) {
ActivityManager manager = (ActivityManager) mActivity.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
1条答案
按热度按时间but5z9lq1#
您可以使用“org.greenrobot:eventbus”将当前GPS位置从前台服务发送到主Activity。
eventbus中的自定义事件声明:
从前台服务发送GPS数据:
接收gps数据的主要活动:
顺祝商祺!
博扬·伊利耶夫