Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android onLocationChanged()仅能在5.0.2版本中工作?_Android_Gps_Location_Locationlistener - Fatal编程技术网

Android onLocationChanged()仅能在5.0.2版本中工作?

Android onLocationChanged()仅能在5.0.2版本中工作?,android,gps,location,locationlistener,Android,Gps,Location,Locationlistener,我正在尝试使用GPS_提供程序获取位置。它在android版本5.0.2中运行良好,但在其他版本中不工作 我提供代码 import android.app.Activity; import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location

我正在尝试使用GPS_提供程序获取位置。它在android版本5.0.2中运行良好,但在其他版本中不工作

我提供代码

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;


import android.provider.Settings;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {
    private LocationManager locationManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      //  startService(new Intent(MainActivity.this, GPSTracker.class));
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                2000, 1, this);
       // SeviceConnHandler serConHandler = new SeviceConnHandler(MainActivity.this);
    }

    @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);
    }

    @Override
    public void onLocationChanged(Location location) {

        String msg = "New Latitude: " + location.getLatitude()
                + "New Longitude: " + location.getLongitude();

        Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderDisabled(String provider) {

        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
        Toast.makeText(getBaseContext(), "Gps is turned off!! ",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {

        Toast.makeText(getBaseContext(), "Gps is turned on!! ",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}
这是我的毕业档案

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.prajeeshthottathil.myapplication"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.android.support:support-v4:18.0.+'
    }

它与网络提供商完美配合。但只需要使用GPS。请告诉我我做错了什么。

请尝试按照此指南使用融合定位api,该api建议通过谷歌获取用户位置。这是一个离线应用程序。在没有互联网连接的情况下工作。fused api是否可以离线工作?是的,它在没有网络连接的情况下工作。它的主要优点是,您不必为选择位置提供程序而烦恼,它会返回给定首选项的位置。请尝试遵循此融合位置api指南,该api建议通过谷歌获取用户的位置。这是一个离线应用程序。在没有互联网连接的情况下工作。fused api是否可以离线工作?是的,它在没有网络连接的情况下工作。它的主要优点是,您不需要麻烦地选择位置提供者,它会返回给定首选项的位置。