模拟分配问题

时间:2019-04-01 15:02:11

标签: android mocking allocation

我正在尝试为我的所有应用构建FakeGPS,但我的apk并未出现在模拟分配应用中。

我使用服务创建了一个主活动,该服务在清单中。

我该怎么办?

编辑:我放入了源代码(即Android中的新程序,谢谢您的帮助!)

Manifest.xml

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

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCES_MOCK_LOCATION" />

    <application
        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"
        tools:ignore="AllowBackup,GoogleAppIndexingWarning">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <service android:name=".mockLocationService" />
    </application>
</manifest>

MainActivity.java

package com.example.fakegps;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.maps.model.LatLng;


public class MainActivity extends AppCompatActivity
{
    public static LatLng latlong = new LatLng(10,10);

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            requestPermissions(new  String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            return;
        }
        else
        {
            startProveedorUbicacion();
        }
    }

    @Override
    protected void onStart()
    {
        super.onStart();
    }

    @Override
    protected void onStop()
    {
        super.onStop();
    }

    public void startProveedorUbicacion()
    {
        Intent intent = new Intent(this, mockLocationService.class);
        intent.putExtra(mockLocationService.LAT_LNG.toString(), latlong);
        startService(intent);
    }

    public void permissionsRequest(int codigoPeticion, String permisos[], int[] resultado)
    {
        if(resultado.length > 1 && resultado[0] == PackageManager.PERMISSION_GRANTED)
        {
            Toast.makeText(this, "Tienes permisos de localizacion", Toast.LENGTH_SHORT).show();
            startProveedorUbicacion();
        }
        else
        {
            Toast.makeText(this, "No tienes permisos de localizacion", Toast.LENGTH_SHORT).show();
            finish();
        }

    }
}

mockLocationService.java

package com.example.fakegps;

import android.app.Service;
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.os.IBinder;
import android.os.SystemClock;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.model.LatLng;

public class mockLocationService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
{
    public static LatLng LAT_LNG;
    private String locationProviderName = "FakeGPS";
    private GoogleApiClient apiClient = null;
    private Location loc;

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        String latlong = intent.getParcelableExtra(MainActivity.latlong.toString());
        if(latlong == null)
        {
            this.stopSelf();
            return Service.START_REDELIVER_INTENT;
        }

        createApi();
        apiClient.connect();

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        pepareProvider(lm);
        lm.isProviderEnabled(locationProviderName);
        actualizarLocalizacion(lm);

        return Service.START_REDELIVER_INTENT;
    }

    private void createApi()
    {
         apiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    void pepareProvider(LocationManager lm)
    {
        lm.addTestProvider(locationProviderName, false, false, false, false, true, true, true, 0, 0);
        lm.setTestProviderEnabled(locationProviderName, true);
    }

    public void addConnectionCallbacks()
    {

    }

    @Override
    public void onConnected(Bundle bundle)
    {

        LocationServices api = (LocationServices) LocationServices.FusedLocationApi;

        ((FusedLocationProviderApi) api).setMockMode(apiClient,true);
        loc = createLocation();
        ((FusedLocationProviderApi) api).setMockLocation(apiClient, loc);
    }

    @Override
    public void onConnectionSuspended(int i) {}

    private Location createLocation()
    {
        loc = new Location(locationProviderName);
        loc.setLatitude(LAT_LNG.latitude);
        loc.setLongitude(LAT_LNG.longitude);
        loc.setAccuracy(0);
        loc.setTime(SystemClock.elapsedRealtimeNanos());
        return loc;
    }

    private void actualizarLocalizacion(LocationManager lm)
    {
        lm.setTestProviderLocation(locationProviderName, loc);
    }


    @Override
    public void onLocationChanged(Location location) {}

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

build.gradle

 apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.fakegps"
        minSdkVersion 27
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.android.gms:play-services-maps:8.1.0'
    implementation  'com.google.android.gms:play-services:8.1.0'
}

0 个答案:

没有答案