无法在 Genymotion VM 中获取位置

Posted

技术标签:

【中文标题】无法在 Genymotion VM 中获取位置【英文标题】:Unable to get Location in Genymotion VM 【发布时间】:2015-08-13 04:41:49 【问题描述】:

我创建了这个简单的应用程序来获取位置。我正在使用 genymotion 虚拟机。我已启用 GPS 并正确安装了 google play 服务。不幸的是,我无法检索位置。当然,我已经添加了正确的权限。还有什么我应该做的吗?

package com.salvo.weather.android;

import android.app.Activity;
import android.location.Location;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;


public class TodayActivity extends Activity
        implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener 

    protected static final String TAG = "location-weather";

    protected Location mLastLocation;
    /**
     * Provides the entry point to Google Play services.
     */
    protected GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_today);

        // TODO: here get coordinates data and pass to Valley JsonObjectRetrieve

        buildGoogleApiClient();
    

    /**
     * Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API.
     */
    protected synchronized void buildGoogleApiClient() 
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    

    @Override
    protected void onStart() 
        super.onStart();
        mGoogleApiClient.connect();
    

    @Override
    protected void onStop() 
        super.onStop();
        if (mGoogleApiClient.isConnected()) 
            mGoogleApiClient.disconnect();
        
    

    @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_today, 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);
    

    /**
     * Runs when a GoogleApiClient object successfully connects.
     */
    @Override
    public void onConnected(Bundle bundle) 

        mLastLocation = LocationServices
                            .FusedLocationApi
                            .getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) 
            Log.i(TAG, String.valueOf(mLastLocation.getLatitude()));
            Log.i(TAG, String.valueOf(mLastLocation.getLongitude()));
         else 
            Toast.makeText(this, "No Location detected", Toast.LENGTH_LONG).show();
        
    

    @Override
    public void onConnectionSuspended(int i) 
        // The connection to Google Play services was lost for some reason. We call connect() to
        // attempt to re-establish the connection.
        mGoogleApiClient.connect();
    

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) 
        // Refer to the javadoc for ConnectionResult to see what error codes might be returned in
        // onConnectionFailed.
        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode());
    

【问题讨论】:

这个问题你解决了吗? 【参考方案1】:

Genymotion 有一项免费功能,可通过模拟器窗口右上角的图标设置名为“GPS”的位置。使用“GPS”图标通过纬度/经度或可搜索地图设置您的位置。然后,定位服务会找到您指定的位置。

报告的其他解决方案包括免费的可编写脚本的 Genyshell 或付费的 Java API。

【讨论】:

以上是关于无法在 Genymotion VM 中获取位置的主要内容,如果未能解决你的问题,请参考以下文章

Genymotion 上的 Google 位置

Hyper-v 与Oracle VM VirtualBox 之间的冲突问题

sh genymotion muleto VM

sh genymotion drago VM

sh genymotion rho VM

sh genymotion rhocon VM