Hi,

I have got a problem with my mock location service.
It should mock the location for an other app, but it only works for google 
maps.
I know that the "other app" is not the problem, because "Fake GPS" is able 
to mock it.

AndroidManifest.xml:

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

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <service
            android:name=".MockService"
            android:enabled="true"
            android:exported="true"
            android:icon="@drawable/ic_check_circle_black_24px"></service>
    </application>

</manifest>

MockService:
public class MockService extends Service implements 
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener 
{

    private GoogleApiClient mGoogleApiClient;
    private Location mockLocation;

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


    @Override
    public void onCreate() {

        // Create an instance of GoogleAPIClient.
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }
    }

    @Override
    public void onDestroy() {
        if (ActivityCompat.checkSelfPermission(this, 
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, 
Manifest.permission.ACCESS_COARSE_LOCATION) != 
PackageManager.PERMISSION_GRANTED) {
        } else {
            LocationServices.FusedLocationApi.setMockMode(mGoogleApiClient, 
false);
        }
        mGoogleApiClient.disconnect();
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mGoogleApiClient.connect();
        return super.onStartCommand(intent,flags,startId);
    }

    private void mockLocation(double latitude, double longitude, float speed) {
        if (ActivityCompat.checkSelfPermission(this, 
android.Manifest.permission.ACCESS_FINE_LOCATION) != 
PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, 
Manifest.permission.ACCESS_COARSE_LOCATION) != 
PackageManager.PERMISSION_GRANTED) {
        } else {
            mockLocation = new Location(LocationManager.NETWORK_PROVIDER);
            mockLocation.setLatitude(latitude);
            mockLocation.setLongitude(longitude);
            mockLocation.setSpeed(speed);
            mockLocation.setAltitude(0);
            mockLocation.setAccuracy(500);
            mockLocation.setTime(System.currentTimeMillis());
            
mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
            mockLocation.setBearing(10);
            mockLocation.setProvider(LocationManager.NETWORK_PROVIDER);

            LocationServices.FusedLocationApi.setMockMode(mGoogleApiClient, 
true);
            LocationServices.FusedLocationApi.setMockLocation(mGoogleApiClient, 
mockLocation);
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        mockLocation(48.133357, 11.573496, 0);
    }

    @Override
    public void onConnectionSuspended(int i) {
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    }
}

call from activity:
public void run(View view) {
    statusTextView.setText("running");
    intent = new Intent(this, MockService.class);
    startService(intent);
}

Thank you for your help


-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/651d07f2-b771-42db-a2d2-0eb4165f0be5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to