0

我是Android开发的初学者。我必须使用分布式位置估计算法开发一个用于 GPS 位置估计的 Android 应用程序。它使用相邻节点的位置信息并计算当前的 GPS 位置。我通过 android 位置文档http://developer.android.com/reference/android/location/package-summary.html了解到推荐使用 Google Location Services API。我在这里浏览了很多帖子,但仍然处于阴霾中。欢迎任何意见。

MainActivity.java

package com.example.sxn8837.locationapi;



import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import static com.example.sxn8837.locationapi.R.*;
import static com.example.sxn8837.locationapi.R.layout.activity_main;
import static com.google.android.gms.location.LocationServices.*;

public class MainActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,LocationListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
    private Location mLastLocation;
    private GoogleApiClient mGoogleApiClient;
    private boolean mRequestLocationUpdates = false ;
    private LocationRequest mLocationRequest;
    private static int UPDATE_INTERVAL = 10000;
    private static int FASTEST_INTERVAL = 5000;
    private static int DISPLACEMENT = 10;




    private TextView lbLocation;
    private Button btnStartLocationUpdates = (Button) findViewById(id.buttonLocationUpdates);
    private Button btnShowLocation = (Button) findViewById(id.buttonShowLocation);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_main);
        lbLocation = (TextView) findViewById(id.lblLocation);
        if(checkPlayServices()){
         buildGoogleApiClient();
         createLocationRequest();
        }

        btnShowLocation.setOnClickListener(new View.OnClickListener (){

            public void onClick(View v){

                displayLocation();

            }
        });

        btnStartLocationUpdates.setOnClickListener(new View.OnClickListener(){

            public void onClick( View v){

                togglePeriodLocationUpdates();
            }

        });
    }

    protected void onStart(){
        super.onStart();
        if(mGoogleApiClient != null){
            mGoogleApiClient.connect();
        }
    }

    protected void onResume(){
        super.onResume();
        checkPlayServices();
        if(mGoogleApiClient.isConnected() && mRequestLocationUpdates ){
            startLocationUpdates();
        }
    }

    protected void onStop(){
        super.onStop();
        if(mGoogleApiClient.isConnected()  ){
            mGoogleApiClient.disconnect();
        }

    }

    protected void onPause(){
        super.onPause();
        stopLocationUpdates();
    }

    private void displayLocation(){

        mLastLocation = FusedLocationApi.getLastLocation(mGoogleApiClient);
        if(mLastLocation !=  null){
            double latitude = mLastLocation.getLatitude();
            double longitude = mLastLocation.getLongitude();
            lbLocation.setText(latitude + "," +longitude);
        }
        else {
            lbLocation.setText("Couldn't get Location Updates");
        }
    }

    private void togglePeriodLocationUpdates(){
        if(!mRequestLocationUpdates) {
            btnStartLocationUpdates.setText(getString(string.btn_stop_location_update));

            mRequestLocationUpdates = true;

            startLocationUpdates();
        }

        else {

            btnStartLocationUpdates.setText(getString(string.btn_stop_location_update));

            mRequestLocationUpdates = false ;

            stopLocationUpdates();


        }
    }

    protected synchronized void buildGoogleApiClient(){

        mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this)
                           .addApi(API).build();
    }

    protected void createLocationRequest(){
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setInterval(FASTEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
    }

    private boolean checkPlayServices(){
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        if(resultCode != ConnectionResult.SUCCESS){

            if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)){
           GooglePlayServicesUtil.getErrorDialog(resultCode,this,PLAY_SERVICES_RESOLUTION_REQUEST).show();

            }else{

                Toast.makeText(getApplicationContext(),"This Device is not supported", Toast.LENGTH_LONG).show();
                finish();
            }
         return false ;
        }

        return true;
    }

    protected void startLocationUpdates(){
        FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, (com.google.android.gms.location.LocationListener) this);
    }

    protected void stopLocationUpdates (){
        final PendingResult<Status> statusPendingResult = FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) 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 onConnected(Bundle bundle) {

        displayLocation();

        if(mRequestLocationUpdates){
            startLocationUpdates();
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
     mGoogleApiClient.connect();
    }

    @Override
    public void onLocationChanged(Location location) {

        mLastLocation = location ;

        Toast.makeText(getApplicationContext(),"Location Changed",Toast.LENGTH_SHORT).show();
        displayLocation();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
       Log.i(TAG, "Connection Failed:" + connectionResult.getErrorCode());
    }
}

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:layout_gravity="center_horizontal"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <TextView android:text="@string/lbl_you_are_at"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize = "25sp"
        android:textStyle = "bold"/>

    <TextView
        android:id="@+id/lblLocation"
        android:padding="15dp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize = "16sp"
        android:textStyle = "bold"/>

    <Button
        android:id="@+id/buttonShowLocation"
        android:padding="20dp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginTop="40dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text = "@string/btn_get_location"
        android:textSize = "16sp"
        android:textStyle = "bold"/>
    >

    <Button
        android:id="@+id/buttonLocationUpdates"
        android:padding="20dp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginTop="60dp"
        android:background="@color/common_signin_btn_default_background"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text = "@string/btn_start_location_update"
        android:textSize = "16sp"
        android:textStyle = "bold"/>
        >

</LinearLayout>

Androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sxn8837.locationapi" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >

        <meta-data
            android:name = "com.google.android.gms.version"
            android:value = "@integer/google_play_services_version"
            />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

日志猫

11-09 20:18:13.352 3645-3645/? D/dalvikvm: Not late-enabling CheckJNI (already on)
11-09 20:18:15.012 3645-3645/com.example.sxn8837.locationapi I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-09 20:18:15.022 3645-3645/com.example.sxn8837.locationapi W/dalvikvm: VFY: unable to resolve virtual method 511: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-09 20:18:15.022 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-09 20:18:15.032 3645-3645/com.example.sxn8837.locationapi I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-09 20:18:15.032 3645-3645/com.example.sxn8837.locationapi W/dalvikvm: VFY: unable to resolve virtual method 533: Landroid/content/res/TypedArray;.getType (I)I
11-09 20:18:15.032 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-09 20:18:15.312 3645-3645/com.example.sxn8837.locationapi I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzh
11-09 20:18:15.312 3645-3645/com.example.sxn8837.locationapi W/dalvikvm: VFY: unable to resolve virtual method 438: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
11-09 20:18:15.312 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: VFY: replacing opcode 0x6e at 0x000b
11-09 20:18:15.332 3645-3645/com.example.sxn8837.locationapi W/GooglePlayServicesUtil: Google Play services is missing.
11-09 20:18:15.502 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: GC_FOR_ALLOC freed 158K, 11% free 3301K/3692K, paused 83ms, total 86ms
11-09 20:18:16.002 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: GC_FOR_ALLOC freed 3K, 11% free 3451K/3848K, paused 23ms, total 23ms
11-09 20:18:16.022 3645-3645/com.example.sxn8837.locationapi I/dalvikvm-heap: Grow heap (frag case) to 5.278MB for 1764376-byte allocation
11-09 20:18:16.052 3645-3654/com.example.sxn8837.locationapi D/dalvikvm: GC_FOR_ALLOC freed <1K, 8% free 5174K/5572K, paused 29ms, total 29ms
11-09 20:18:16.282 3645-3645/com.example.sxn8837.locationapi W/GooglePlayServicesUtil: Google Play services is missing.
11-09 20:18:16.332 3645-3645/com.example.sxn8837.locationapi D/AndroidRuntime: Shutting down VM
11-09 20:18:16.332 3645-3645/com.example.sxn8837.locationapi W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb1adbba8)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: FATAL EXCEPTION: main
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: Process: com.example.sxn8837.locationapi, PID: 3645
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {com.example.sxn8837.locationapi/com.example.sxn8837.locationapi.MainActivity}: java.lang.NullPointerException
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2774)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:  Caused by: java.lang.NullPointerException
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at com.example.sxn8837.locationapi.MainActivity.onResume(MainActivity.java:86)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.Activity.performResume(Activity.java:5310)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

 

4

2 回答 2

1

您可以使用Google 的 Android 融合位置 API

Fused Location Provider 智能地管理底层定位技术,并根据我们的需求为我们提供最佳位置。

为什么使用:

我们可以选择其中一个位置提供商(网络或 GPS)并请求位置更新或设置接近警报。但是这种方法有两个主要问题:

  1. 如果我们需要定义精确的位置,我们必须在网络和 GPS 位置提供者之间切换(因为 GPS 不能在室内工作)。
  2. 接近警报用于通知用户接近某个位置,这会影响电池寿命。

使用此 API 的优势

  1. 简单的 API:让我们指定“高精度”或“低功耗”等高级需求,而不必担心位置提供者。
  2. 立即可用:让我们的应用程序可以立即访问最近的最佳位置。
  3. 电源效率:最大限度地减少应用程序的电源使用。基于所有传入的位置请求和可用的传感器,融合位置提供者选择最有效的方式来满足这些需求。
  4. 多功能性:满足广泛的需求,从需要高精度定位的前台使用到需要定期更新位置且功耗影响可忽略不计的后台使用。
于 2015-10-20T06:05:01.460 回答
0

Google 位置服务 API 是 Google Play 服务的一部分,它提供了一个更强大的高级框架,可以自动处理位置提供者、用户移动和位置准确性。它还根据您提供的功耗参数处理位置更新调度。在大多数情况下,您将通过使用定位服务 API 获得更好的电池性能以及更合适的准确度。

http://developer.android.com/guide/topics/location/strategies.html

于 2015-10-20T04:17:30.053 回答