使用Gcm通知设置模拟位置

时间:2014-12-09 17:34:31

标签: android google-cloud-messaging android-location

每当我点击谷歌地图调试工具上的某个位置时,我都会尝试将gps位置伪装到手机中。 为此,我创建了一个gcm通知项目,并将通知推送到我的手机。 我有以下IntentService假定设置模拟位置。 但它不能正常工作。 假电子GPS应用在我的手机中正常工作。

非常感谢任何帮助。

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;

public class GcmIntentService extends IntentService  implements LocationListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    String TAG="GcmIntentService";

    public LocationClient mLocationClient;

    private static final String PROVIDER = "flp";
    private static final double LAT = 37.377166;
    private static final double LNG = -122.086966;
    private static final float ACCURACY = 3.0f;


    public GcmIntentService() {
        super("GcmIntentService");
        // Connect to Location Services
        }

    @Override
    public void onCreate()
    {
        super.onCreate();
        mLocationClient = new LocationClient(this.getApplicationContext(), this, this);
        mLocationClient.connect();
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
     //   emitLocation.sendLocation(location);
        Log.d("GcmIntentService", "Location changed to "+location.toString());
        Toast.makeText(getApplicationContext(), "Location changed to "+location.toString(), Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */
            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " +
                        extras.toString());
                // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
//                for (int i=0; i<5; i++) {
//                    Log.i(TAG, "Working... " + (i + 1)
//                            + "/5 @ " + SystemClock.elapsedRealtime());
//                    try {
//                        Thread.sleep(5000);
//                    } catch (InterruptedException e) {
//                    }
//                }
                //Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification("Received: " + extras.toString());
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.track)
                        .setContentTitle("GCM Notification")
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(alarmSound);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

    public Location createLocation(double lat, double lng, float accuracy) {
        // Create a new Location
        Location newLocation = new Location(PROVIDER);
        newLocation.setLatitude(lat);
        newLocation.setLongitude(lng);
        newLocation.setAccuracy(accuracy);
        return newLocation;
    }

    @Override
    public void onConnected(Bundle bundle) {
        mLocationClient.setMockMode(true);
        Location testLocation = createLocation(LAT, LNG, ACCURACY);
        mLocationClient.setMockLocation(testLocation);
        Toast.makeText(getApplicationContext(), "set Mock Location", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDisconnected() {
              Toast.makeText(getApplicationContext(), "connection disconnected", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.e("GcmIntentService", "Connection Failed: "+connectionResult.toString());

    }

    // Example of creating a new Location from test data

}

1 个答案:

答案 0 :(得分:0)

以下官方教程中的文档缺少重要的一行。 http://developer.android.com/training/location/location-testing.html

newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());至关重要。 修改以下函数后,模拟位置工作正常。

public Location createLocation(double lat, double lng, float accuracy) {
        // Create a new Location
        Location newLocation = new Location(LocationManager.GPS_PROVIDER);

        newLocation.setLatitude(lat);
        newLocation.setLongitude(lng);
        newLocation.setProvider(LocationManager.GPS_PROVIDER);
        newLocation.setAccuracy(1);
        newLocation.setTime(new Date().getTime());
        newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
        return newLocation;
    }