1. derEisige, 15.04.2019 #1
    derEisige

    derEisige Threadstarter Neuer Benutzer

    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
    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);
        }
    }
    TrackerService
    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;
            }
        }
    }
     
  2. jogimuc, 15.04.2019 #2
    jogimuc

    jogimuc Fortgeschrittenes Mitglied

  3. derEisige, 15.04.2019 #3
    derEisige

    derEisige Threadstarter Neuer Benutzer

    aber die prüfe ich doch im Konstruktor
     
  4. jogimuc, 15.04.2019 #4
    jogimuc

    jogimuc Fortgeschrittenes Mitglied

    Nein leider nicht. Schaue dir den link mal an.

    Zeig mal dein logcat
     
  5. derEisige, 15.04.2019 #5
    derEisige

    derEisige Threadstarter Neuer Benutzer

    --------- beginning of main
    2019-04-15 19:31:16.497 824-835/? I/mmid: select timeout: wait for receiving msg
    2019-04-15 19:31:17.621 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2686
    2019-04-15 19:31:17.634 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2633
    2019-04-15 19:31:17.650 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2579
    2019-04-15 19:31:17.669 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2526
    2019-04-15 19:31:17.687 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2473
    2019-04-15 19:31:17.707 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2419
    2019-04-15 19:31:17.719 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2366
    2019-04-15 19:31:17.735 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2313
    2019-04-15 19:31:17.750 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2259
    2019-04-15 19:31:17.768 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2206
    2019-04-15 19:31:17.788 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2152
    2019-04-15 19:31:17.804 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2099
    2019-04-15 19:31:17.824 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=2046
    2019-04-15 19:31:17.837 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1992
    2019-04-15 19:31:17.857 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1939
    2019-04-15 19:31:17.870 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1886
    2019-04-15 19:31:17.891 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1832
    2019-04-15 19:31:17.902 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1779
    2019-04-15 19:31:17.921 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1726
    2019-04-15 19:31:17.938 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1672
    2019-04-15 19:31:17.956 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1619
    2019-04-15 19:31:17.970 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1566
    2019-04-15 19:31:17.990 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1512
    2019-04-15 19:31:18.004 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1459
    2019-04-15 19:31:18.020 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1405
    2019-04-15 19:31:18.040 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1352
    2019-04-15 19:31:18.054 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1299
    2019-04-15 19:31:18.071 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1245
    2019-04-15 19:31:18.088 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1192
    2019-04-15 19:31:18.104 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1139
    2019-04-15 19:31:18.121 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1085
    2019-04-15 19:31:18.137 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=1032
    2019-04-15 19:31:18.157 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=979
    2019-04-15 19:31:18.168 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=925
    2019-04-15 19:31:18.188 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=872
    2019-04-15 19:31:18.203 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=819
    2019-04-15 19:31:18.221 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=765
    2019-04-15 19:31:18.239 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=712
    2019-04-15 19:31:18.254 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=658
    2019-04-15 19:31:18.274 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=605
    2019-04-15 19:31:18.286 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=552
    2019-04-15 19:31:18.307 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=498
    2019-04-15 19:31:18.318 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=445
    2019-04-15 19:31:18.337 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=392
    2019-04-15 19:31:18.356 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=338
    2019-04-15 19:31:18.371 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=285
    2019-04-15 19:31:18.388 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=232
    2019-04-15 19:31:18.404 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=178
    2019-04-15 19:31:18.421 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=125
    2019-04-15 19:31:18.435 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=72
    2019-04-15 19:31:18.456 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=18
    2019-04-15 19:31:17.604 3540-3575/? I/chatty: uid=1000(system) PowerManagerSer identical 1 line
    2019-04-15 19:31:18.470 3540-3575/? D/AutomaticBrightnessController: luxvalue = 1.8437195
    2019-04-15 19:31:18.470 3540-3575/? D/DisplayPowerController: getScreenDarkenRate: brighten rate=40
    2019-04-15 19:31:18.470 3540-3575/? D/AutomaticBrightnessController: luxvalue = 1.8437195
    2019-04-15 19:31:18.475 735-735/? D/qdlights: entry write_int path = /sys/class/leds/lcd-backlight/brightness, value=16
    2019-04-15 19:31:18.712 3540-4851/? D/CompatibilityInfo: mCompatibilityFlags - 0
    2019-04-15 19:31:18.712 3540-4851/? D/CompatibilityInfo: applicationDensity - 440
    2019-04-15 19:31:18.712 3540-4851/? D/CompatibilityInfo: applicationScale - 1.0
    2019-04-15 19:31:19.500 824-835/? I/mmid: select timeout: wait for receiving msg
    2019-04-15 19:31:19.577 3540-4851/? I/WifiService: getWifiEnabledState uid=10019
    2019-04-15 19:31:19.579 3540-4851/? I/WifiService: getConnectionInfo uid=10019
    2019-04-15 19:31:19.737 3540-4851/? D/CompatibilityInfo: mCompatibilityFlags - 0
    2019-04-15 19:31:19.737 3540-4851/? D/CompatibilityInfo: applicationDensity - 440
    2019-04-15 19:31:19.737 3540-4851/? D/CompatibilityInfo: applicationScale - 1.0
    2019-04-15 19:31:19.931 3540-4770/? D/CompatibilityInfo: mCompatibilityFlags - 0
    2019-04-15 19:31:19.931 3540-4770/? D/CompatibilityInfo: applicationDensity - 440
    2019-04-15 19:31:19.931 3540-4770/? D/CompatibilityInfo: applicationScale - 1.0
    2019-04-15 19:31:18.471 3540-3575/? D/AutomaticBrightnessController: luxvalue = 1.8437195
    2019-04-15 19:31:20.565 3540-4770/? I/ActivityManager: Killing 16040:com.miui.audioeffect/u0a35 (adj 906): empty #11
    2019-04-15 19:31:20.567 4931-5377/? D/PowerKeeper.Event: notifyActiveKilled processName: com.miui.audioeffect, pid:16040, reason:empty #11
    2019-04-15 19:31:20.569 3540-3566/? W/zygote64: kill(-16040, 9) failed: No such process
    2019-04-15 19:31:20.570 3540-4770/? I/ActivityManager: Killing 16166:com.xiaomi.providers.appindex/u0a53 (adj 906): empty #12
    2019-04-15 19:31:20.572 4931-5377/? D/PowerKeeper.Event: notifyActiveKilled processName: com.xiaomi.providers.appindex, pid:16166, reason:empty #12
    2019-04-15 19:31:20.572 3540-4770/? D/CompatibilityInfo: mCompatibilityFlags - 0
    2019-04-15 19:31:20.572 3540-4770/? D/CompatibilityInfo: applicationDensity - 440
    2019-04-15 19:31:20.572 3540-4770/? D/CompatibilityInfo: applicationScale - 1.0
    2019-04-15 19:31:20.576 3540-3566/? W/zygote64: kill(-16040, 9) failed: No such process
    2019-04-15 19:31:20.588 3540-3566/? I/chatty: uid=1000(system) ActivityManager identical 2 lines
    2019-04-15 19:31:20.593 3540-3566/? W/zygote64: kill(-16040, 9) failed: No such process
    2019-04-15 19:31:20.601 4931-5377/? D/PowerKeeper.Event: notifyAMProcDied pacakageName: com.miui.audioeffect, pid:16040
    2019-04-15 19:31:20.601 3540-3566/? W/zygote64: kill(-16040, 9) failed: No such process
    2019-04-15 19:31:20.607 3540-3566/? W/zygote64: kill(-16040, 9) failed: No such process
    2019-04-15 19:31:20.607 3540-3566/? I/zygote64: Successfully killed process cgroup uid 10035 pid 16040 in 37ms
    2019-04-15 19:31:20.607 3540-3566/? W/zygote64: kill(-16166, 9) failed: No such process
    2019-04-15 19:31:20.607 4931-5377/? D/PowerKeeper.Event: notifyAMProcDied pacakageName: com.xiaomi.providers.appindex, pid:16166
    2019-04-15 19:31:20.613 3540-3566/? W/zygote64: kill(-16166, 9) failed: No such process
    2019-04-15 19:31:20.613 3540-3566/? I/zygote64: Successfully killed process cgroup uid 10053 pid 16166 in 6ms
    2019-04-15 19:31:21.658 5107-13808/? W/MdnsDeviceScannerEntry: Invalid remote control notifications enabled status; 0
    2019-04-15 19:31:21.660 3540-4844/? I/WifiService: getWifiEnabledState uid=10019
    2019-04-15 19:31:21.661 3540-4844/? I/WifiService: getConnectionInfo uid=10019
    2019-04-15 19:31:21.665 3540-4844/? I/WifiService: getWifiEnabledState uid=10019
    2019-04-15 19:31:21.665 3540-4844/? I/WifiService: getConnectionInfo uid=10019
    2019-04-15 19:31:21.668 3540-4844/? I/WifiService: getWifiEnabledState uid=10019
    2019-04-15 19:31:21.669 3540-4844/? I/WifiService: getConnectionInfo uid=10019
    2019-04-15 19:31:21.671 3540-4844/? I/WifiService: getWifiEnabledState uid=10019
    2019-04-15 19:31:21.672 3540-4844/? I/WifiService: getConnectionInfo uid=10019
    2019-04-15 19:31:22.504 824-835/? I/mmid: select timeout: wait for receiving msg
    -- Dieser Beitrag wurde automatisch mit dem folgenden Beitrag zusammengeführt --
    Kann ich die Permission im Konstruktor des Service abfragen oder muss ich das in der Activity abkragen?
     
  6. swa00, 16.04.2019 #6
    swa00

    swa00 Moderator Team-Mitglied

    Hallo ,

    a) Der Logcat sagt nichts aus - Filter ihn mal bitte nach deinem Paket
    b) jogi meint , dass es nicht reicht innerhalb der App permissions in der Manifest zu setzen , sondern du musst der App im System
    die Erlaubnis durch den User erteilen lassen.
    Request App Permissions | Android Developers

    Setze zur Probe mal die Berechtigungen manuell.

    Anmerkungen :
    1) Falls du einen Tracker bauen möchtest , der im Service GPS Koordinaten 24/7 loggt,
    so wird dies so nicht funktionieren . (Siehe auch GC und LifeCycle)
    2) Tutorials vermitteln nicht die Funktion des Systemes, die bei dieser Anwendung eine erhebliche Rolle spielen.

    Und so ganz am Rande : Für einen "Anfänger" mit wenig Erfahrung gleich mit Services zu beginnen,
    ist nicht ratsam. In deinem Source fehlen elementare Komponenten.
    Copy & Paste bringen dich nicht zum Ziel
     
    Zuletzt bearbeitet: 16.04.2019
    jogimuc bedankt sich.
  7. jogimuc, 16.04.2019 #7
    jogimuc

    jogimuc Fortgeschrittenes Mitglied

    Ja aus deinem log kann man dieser nichts nehmen.
    In deinen log kann ich nicht mal erkennen das der Konstruktor der Server Klasse aufgerufen wird es gibt vnicht mal das log was du dort aufrufst.
    Somit wird der Konstuktor wohl gar nicht aufgerufen. Der Service entspricht auch nicht dem livevcyle.

    Die permission solltest du besten am Anfang der app abfragen also in der oncreate und zwar bevor du irgendwie auf das GPS zugreifen tust.
     
  8. derEisige, 16.04.2019 #8
    derEisige

    derEisige Threadstarter Neuer Benutzer

    Sorry wegen dem log.

    Ich habe jetzt eine Lösung gefunden, habe es leider nicht in den Konstruktor bekommen weil ich ActivityCompat.requestPermissions einfach nicht an die
    MainActivity weitergeben konnte.

    Code:
    public static final String[] myPermissions = new String[]{ Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET };
        public static final int myRequestCode = 0;
    
       protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // view aufrufen
            setContentView(R.layout.activity_main);
            Toolbar toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            // Permission
            showPermission();
            for( int i = 0 ; i < myPermissions.length; i++){
                requestPermission(myPermissions[i]);
            }
    }
    // Permission ----------------------
        private void showPermission() {
            for (int i = 0; i < myPermissions.length; i++){
                int permissionCheck = ContextCompat.checkSelfPermission(this, myPermissions[i]);
    
                if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "permission granted", Toast.LENGTH_LONG).show();
                }
                if (permissionCheck == PackageManager.PERMISSION_DENIED) {
                    Toast.makeText(this, "permission denid", Toast.LENGTH_LONG).show();
                }
            }
        }
    
        private void requestPermission(String mPermissions) {
            int permissionCheck = ContextCompat.checkSelfPermission(this, mPermissions);
            if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
                return;
            }
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, mPermissions)) {
                // Erklärung anzeigen
            }
            ActivityCompat.requestPermissions(this, new String[]{mPermissions} , myRequestCode);
    
        }
     
  9. jogimuc, 16.04.2019 #9
    jogimuc

    jogimuc Fortgeschrittenes Mitglied

    In der Main activity gibt es keinen Konstruktor dafür bist die onCreate da.

    Schaue dir mal die n livevcyle einer activity an.
    .
     
  10. ludy, 16.04.2019 #10
    ludy

    ludy App-Anbieter (kostenlos)

    swa00 bedankt sich.
Die Seite wird geladen...
Ähnliche Themen Forum Datum
Zuverlässige GPS abfragen Android App Entwicklung 13.12.2018
GPS Lokalisierung Android App Entwicklung 19.11.2018
Du betrachtest das Thema "Positionsbestimmung GPS/ Provider" im Forum "Android App Entwicklung",
  1. Android-Hilfe.de verwendet Cookies um Inhalte zu personalisieren und dir den bestmöglichen Service zu gewährleisten. Wenn du auf der Seite weitersurfst stimmst du der Cookie-Nutzung zu.  Ich stimme zu.