android 模拟位置信息Location使用示例

android 自带location除了可以输出gps的经纬度信息,还可以进行传入location数据,进行模拟输出。输出模拟的位置信息可以在同一个应用程序,也可以给其他应用app使用。

本文的源码下载:http://download.csdn.net/detail/qq_16064871/9857036

1,开启传入location信息

先打开系统本机的gps,然后去开发者选项里打开模拟位置服务

 

    //打开GPS设置
    public void OpenGpsSettingEvent() {
        Intent callGPSSettingIntent = new Intent(
                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        mContext.startActivity(callGPSSettingIntent);
    }

 

    //打开开发者模式
    public void openTestProviderLocationException() {
        Intent intent = new Intent("//");
        ComponentName cm = new ComponentName("com.android.settings", "com.android.settings.DevelopmentSettings");
        intent.setComponent(cm);
        intent.setAction("android.intent.action.VIEW");
        mContext.startActivity(intent);
    }

 

 

 

 

 

Center

添加模拟数据      mLocationManager.setTestProviderStatus(LocationManager.GPS_PROVIDER, 100, bundle, System.currentTimeMillis());其中100是自定义的标志位,也可以携带自定义的bundle数据。

 

    //启动模拟位置服务
    public boolean initLocation() {
        mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            return false;
        }
        try {
            //如果未开启模拟位置服务,则添加模拟位置服务
            mLocationManager.addTestProvider(LocationManager.GPS_PROVIDER, false, false, false, false, true, true, true, 0, 5);
            mLocationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);

        } catch (Exception e) {
            return false;
        }
        return true;
    }

    //停止模拟位置服务
    public void stopMockLocation() {
        mbUpdate = false;

        if (mLocationManager != null) {
            try {
                mLocationManager.clearTestProviderEnabled(LocationManager.GPS_PROVIDER);
                mLocationManager.removeTestProvider(LocationManager.GPS_PROVIDER);
            } catch (Exception e) {
                Log.e("GPS", e.toString());
            }
        }
    }

    private Bundle bundle = new Bundle();
    double testData = 0.0;

    public void asynTaskUpdateCallBack() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (mbUpdate) {

                    //测试的location数据
                    mlocation.setLongitude(testData++);
                    mlocation.setLatitude(testData++);
                    mlocation.setAltitude(testData++);
                    mlocation.setTime(System.currentTimeMillis());
                    mlocation.setBearing((float) 1.2);
                    mlocation.setSpeed((float) 1.2);
                    mlocation.setAccuracy((float) 1.2);

                    //额外的自定义数据,使用bundle来传递
                    bundle.putString("test1", "666");
                    bundle.putString("test2", "66666");
                    mlocation.setExtras(bundle);
                    try {
                        if (android.os.Build.VERSION.SDK_INT >= 17)
                            mlocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
                        mLocationManager.setTestProviderStatus(LocationManager.GPS_PROVIDER, 100, bundle, System.currentTimeMillis());
                        mLocationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mlocation);

                        Thread.sleep(1000);
                    } catch (Exception e) {
                        return;
                    }
                }

            }
        }).start();
    }

 

 

 

 

 

Center

 

2,获取模拟location数据

模拟location数据跟一般的location数据获取,以及初始化时一样的。唯一不同的是 onStatusChanged("gps", 100, mlocal.getExtras());这个方法使用。

 

package com.example.gpslocationdemo;

import android.annotation.TargetApi;
import android.content.Context;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class LocationTestActivity extends AppCompatActivity {
    LocationManager mLocationManager;
    Location mlocation;
    TextView mTextView;
    TextView mTextView2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_test);

        mTextView = (TextView) findViewById(R.id.textView1);
        mTextView2 = (TextView) findViewById(R.id.textView2);

        getLocation();
    }

    public Location getLocation() {
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
            mLocationManager.addNmeaListener(mNmeaListener);
        }

        return mlocation;
    }


    //这里获取的数据就是在之前一个activity写进去的数据
    LocationListener mLocationListener = new LocationListener() {
        //这个数据是经过LocationManager
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        @Override
        public void onLocationChanged(Location mlocal) {
            if (mlocal == null) return;
            String strResult = "getAccuracy:" + mlocal.getAccuracy() + "\r\n"
                    + "getAltitude:" + mlocal.getAltitude() + "\r\n"
                    + "getBearing:" + mlocal.getBearing() + "\r\n"
                    + "getElapsedRealtimeNanos:" + String.valueOf(mlocal.getElapsedRealtimeNanos()) + "\r\n"
                    + "getLatitude:" + mlocal.getLatitude() + "\r\n"
                    + "getLongitude:" + mlocal.getLongitude() + "\r\n"
                    + "getProvider:" + mlocal.getProvider() + "\r\n"
                    + "getSpeed:" + mlocal.getSpeed() + "\r\n"
                    + "getTime:" + mlocal.getTime() + "\r\n";
            Log.i("Show", strResult);
            if (mTextView != null) {
                mTextView.setText(strResult);
            }

            onStatusChanged("gps", 100, mlocal.getExtras());
        }

        @Override
        public void onProviderDisabled(String arg0) {
        }

        @Override
        public void onProviderEnabled(String arg0) {
        }

        @Override
        public void onStatusChanged(String provider, int event, Bundle extras) {
            if (event ==100){
                String strResult = extras.getString("test1","") +"\n" +
                extras.getString("test2","");

                if (mTextView2 != null) {
                    mTextView2.setText(strResult);
                }
            }

        }
    };

    //原始数据监听
    GpsStatus.NmeaListener mNmeaListener = new GpsStatus.NmeaListener() {
        @Override
        public void onNmeaReceived(long arg0, String arg1) {
            byte[] bytes = arg1.getBytes();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            mLocationManager.removeNmeaListener(mNmeaListener);
            mLocationManager.removeUpdates(mLocationListener);
        }
    }
}

 

 

正常获取location数据可参考:android 自带gps定位Location相关知识

 

Center

3,权限

 

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>

 

 

 

 

 

4,附上部分location的sdk源码

 

    // --- Mock provider support ---
    // TODO: It would be fantastic to deprecate mock providers entirely, and replace
    // with something closer to LocationProviderBase.java

    /**
     * Creates a mock location provider and adds it to the set of active providers.
     *
     * @param name the provider name
     *
     * @throws SecurityException if {@link android.app.AppOpsManager#OPSTR_MOCK_LOCATION
     * mock location app op} is not set to {@link android.app.AppOpsManager#MODE_ALLOWED
     * allowed} for your app.
     * @throws IllegalArgumentException if a provider with the given name already exists
     */
    public void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite,
            boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
            boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
        ProviderProperties properties = new ProviderProperties(requiresNetwork,
                requiresSatellite, requiresCell, hasMonetaryCost, supportsAltitude, supportsSpeed,
                supportsBearing, powerRequirement, accuracy);
        if (name.matches(LocationProvider.BAD_CHARS_REGEX)) {
            throw new IllegalArgumentException("provider name contains illegal character: " + name);
        }

        try {
            mService.addTestProvider(name, properties, mContext.getOpPackageName());
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException", e);
        }
    }


    

 

 

 

 

 

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Fakelocation是一个使用模拟功能的应用程序。这个应用程序可以让用户在移动设备上模拟他们的位置信息。它可以改变设备的GPS定位,使得用户可以在任何地点显示他们所选择的位置。这对于一些特定的需求非常有用,例如在社交媒体上分享位置信息、进行地理位置验证或者测试特定地区的应用程序等。 使用Fakelocation的模拟功能非常简单。用户只需要打开应用程序,并选择他们想要模拟的位置,然后点击确认即可。设备的GPS定位将被修改为所选位置,而不管用户实际所处的地点。这个应用程序还提供了一些额外的功能,例如保存和管理过去的模拟位置,以及设置定时器来自动更改位置。 然而,需要注意的是,使用Fakelocation进行位置模拟有一些潜在的风险和限制。首先,使用该应用程序模拟位置可能违反某些应用程序或平台的使用规定。某些应用程序或平台可能会检测到模拟位置,并可能会采取相应的措施,如封禁用户账号。另外,一些应用程序或服务可能会使用其他方式来验证用户的真实位置,例如通过IP地址或其他传感器数据。因此,Fakelocation并不能保证用户能够成功欺骗这些验证方法。 总而言之,Fakelocation是一个使用模拟功能的应用程序,可以让用户在移动设备上改变他们的GPS定位,并模拟其他位置信息。然而,使用该应用程序需要谨慎,避免违反应用程序或平台的使用规定,以及注意其他验证方式可能泄露真实位置的风险。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值