D
derEisige
Neues Mitglied
- 0
Hallo,
ich habe Gestern mit der Programierung für Androide angefangen.
Nun bekomme ich es nicht hin das GPS anzusprechen, ich habe schon acht Tutorials zum Thema durch bekomme es aber nicht zum laufen.
Ich hoffe mir kann jemand weiterhelfen.
Androide 8.1
Manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity
TrackerService
ich habe Gestern mit der Programierung für Androide angefangen.
Nun bekomme ich es nicht hin das GPS anzusprechen, ich habe schon acht Tutorials zum Thema durch bekomme es aber nicht zum laufen.
Ich hoffe mir kann jemand weiterhelfen.
Androide 8.1
Manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity
Code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TrackerService tracker = new TrackerService(this);
((TextView)findViewById(R.id.gpsLatitude)).setText("Latitude: " + tracker.getLatitude());
((TextView)findViewById(R.id.gpsLongitude)).setText("Longitude: " + tracker.getLongitude());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Code:
public class TrackerService extends Service implements LocationListener {
private Context context;
private LocationManager locationManager;
private Location location = null;
public TrackerService(Context c) {
context = c;
LocationManager locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
boolean enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// check if enabled and if not send user to the GSP settings
// Better solution would be to display a dialog and suggesting to
// go to the settings
Log.d("isProviderEnabled: ", String.valueOf(enabled));
if (!enabled) {
Log.d("isProviderEnabled2: ", String.valueOf(enabled));
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
locate();
}
@SuppressLint("MissingPermission")
private void locate() {
location = null;
try {
boolean isGPSActive = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetActive = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSActive) {
Log.d("GPS", "active");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 10, this);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}else if(isNetActive){
Log.d("NET", "active");
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,30000,10,this);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
public double getLatitude(){
if (location != null){
return (double)location.getLatitude();
}else {
return 0;
}
}
public double getLongitude(){
if (location != null){
return (double)location.getLongitude();
}else {
return 0;
}
}
}