Created methods to send Location to telegram.

This commit is contained in:
Filip Strajnar 2024-10-26 12:25:55 +02:00
parent 79fd58d419
commit 7bdd8fc0b3
2 changed files with 26 additions and 0 deletions

View file

@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

View file

@ -20,11 +20,16 @@ import androidx.core.app.ServiceCompat;
import androidx.core.location.LocationManagerCompat;
import androidx.room.Room;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.Chat;
import com.pengrad.telegrambot.request.SendLocation;
import java.util.List;
public class LocationLoggingService extends Service implements LocationListener {
private final String TAG = LocationLoggingService.class.getName();
private LocationDao locationDao;
private Location lastLocation;
public LocationLoggingService() {
}
@ -88,8 +93,28 @@ public class LocationLoggingService extends Service implements LocationListener
super.onCreate();
}
private void updateLocation(Location newLocation){
lastLocation = newLocation;
sendLocationToTelegram(newLocation);
}
public void sendLocationToTelegram(Location location){
String botToken = getResources().getString(R.string.telegramToken);
String telegramChat = getResources().getString(R.string.telegramChat);
TelegramBot bot = new TelegramBot(botToken);
new Thread(()->{
bot.execute(new SendLocation(telegramChat,
Double.valueOf(location.getLatitude()).floatValue(),
Double.valueOf(location.getLongitude()).floatValue()
));
}).start();
}
@Override
public void onLocationChanged(@NonNull Location location) {
updateLocation(location);
new Thread(()->{
Log.d(TAG, String.format(
"New location. Latitude: %s Longitude: %s Altitude: %s",