2017-09-14 2 views
0

Ich versuche Wetterinformationen mit GPS-Standort zu bekommen, aber das Problem ist Wert für meine Position 0,0 ("Breite, Länge"). Wenn ich auf Einstellung für mein Telefon gehe -> "Erlaubnis" fand keine Erlaubnis erlaubt, und ich muss die Erlaubnis manuell aktivieren. Dann funktioniert die App gut.keine Berechtigungen erlaubt Auto android Studio

manifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.ionicframework.myapp574503"> 

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-feature android:name="android.hardware.location.gps"/> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <application android:name="android.support.multidex.MultiDexApplication" 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

MainActivity

package com.ionicframework.myapp574503; 

import android.location.LocationManager; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.annotation.RequiresApi; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.android.volley.Request; 
import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.JsonObjectRequest; 
import com.android.volley.toolbox.Volley; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 
    RequestQueue rq; 
    TextView timeDesc, windspeedDesc; 
    String wind; 
    GPSTracker gps; 
    LocationManager locationManager; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     windspeedDesc = (TextView) findViewById(R.id.textView1); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 

     // create class object 
     gps = new GPSTracker(MainActivity.this); 

     // check if GPS enabled 
     if(gps.canGetLocation()){ 

      double latitude = gps.getLatitude(); 
      double longitude = gps.getLongitude(); 
      String url = "http://api.wunderground.com/api/xxxxxxxxxxx/conditions/hourly/forecast10day/geolookup/q/"+latitude+","+longitude+".json"; 

      Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
      rq = Volley.newRequestQueue(this); 

      sendjsonrequest(url); 

      // \n is for new line 
     }else{ 
      // can't get location 
      // GPS or Network is not enabled 
      // Ask user to enable GPS/network in settings 
      gps.showSettingsAlert(); 
     } 
    } 

    public void sendjsonrequest(String url) { 
     JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 
      @RequiresApi(api = Build.VERSION_CODES.N) 
      @Override 
      public void onResponse(JSONObject response) { 
       try { 

        JSONObject stationsJO = response.getJSONObject("location"); 
        JSONArray array = stationsJO.optJSONArray("days"); 

        wind= stationsJO.getString("city"); 
        windspeedDesc.setText(wind); 



       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
      } 
     }); 
     rq.add(jsonObjectRequest); 

    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.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); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

ich auf Version 4.4.2 getestet und es funktionierte gut und die Erlaubnis Auto erlaubt, aber auf Version 7.0 Ich habe die Erlaubnis zur Aktivierung manuell von der Einstellung Ideen ?

+0

Kann u der Laufzeit die Erlaubnis geben für ACCESS_FINE_LOCATION? für Android Version 6.0 oder höher? –

Antwort

0

Versuchen Sie diesen Code ActivityCompat.requestPermissions

Es ist ein Tutorial oder höher verwenden

private int PERMISSIONS_REQUEST_LOCATION = 100; 

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_activity); 

     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { 
       if (ContextCompat.checkSelfPermission(MainFragmentActivity.this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainFragmentActivity.this,ACCESS_FINE_LOCATION)) { 

        } else { 
      ActivityCompat.requestPermissions(MainFragmentActivity.this, new String[]{ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_LOCATION); 
        } 
       } 
     } 
} 
+0

danke für Hilfe, aber ich habe rot auf ACCESS_FINE_LOCATION – pabloescobar

+0

Funktioniert das für Sie? –

+0

check img https://e.top4top.net/p_622q62b31.png – pabloescobar

0

Sie müssen die Laufzeitberechtigung anfordern.

Sie tun das auf tun dies in Android Version 6.0 für Runtime Permission here

Verwandte Themen