实例介绍
【实例简介】兼容新机
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
测试流程:
1.首先在手机>>设置>>开发人员选项>>选中 允许模拟地点
2.关掉本机自带的GPS
3.打开该实例 并 startService,这时候 你再去 微信 看下附近的人,已经把你定位到俄罗斯了
【实例截图】
【核心代码】
package com.devspark.mockgps;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.IBinder;
import android.util.Log;
/**
*
* @author johnkil
*
*/
public class MockGpsService extends Service {
private static final String LOG_TAG = MockGpsService.class.getSimpleName();
@SuppressWarnings("rawtypes")
private static final Class[] mStartForegroundSignature = new Class[] {
int.class, Notification.class};
@SuppressWarnings("rawtypes")
private static final Class[] mStopForegroundSignature = new Class[] {
boolean.class};
private static final int NOTIFICATION_ID = R.string.notification;
private NotificationManager mNM;
private Method mStartForeground;
private Method mStopForeground;
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1];
private MockGpsProvider mMockGpsProvider;
@Override
public void onCreate() {
Log.v(LOG_TAG, "onCreate() called");
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
try {
mStartForeground = getClass().getMethod("startForeground", mStartForegroundSignature);
mStopForeground = getClass().getMethod("stopForeground", mStopForegroundSignature);
} catch (NoSuchMethodException e) {
// Running on an older platform.
mStartForeground = mStopForeground = null;
}
startMockLocation();
startForegroundCompat(NOTIFICATION_ID, getNotification(getString(R.string.notification)));
}
// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
Log.v(LOG_TAG, "onStart() called");
handleCommand(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(LOG_TAG, "onStartCommand() called");
if (intent != null) {
handleCommand(intent);
}
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
private void handleCommand(Intent intent) {
String action = intent.getAction();
Log.v(LOG_TAG, String.format("handleCommand() called: action=[%s]", action));
}
private Notification getNotification(String contentText) {
Log.v(LOG_TAG, "getNotification() called");
// In this sample, we'll use the same text for the ticker and the expanded notification
/*CharSequence text = getText(R.string.notification);*/
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.ic_stat_mock_gps, contentText, 0);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MockGpsActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.app_name), contentText, contentIntent);
return notification;
}
/**
* This is a wrapper around the new startForeground method, using the older
* APIs if it is not available.
*/
private void startForegroundCompat(int id, Notification notification) {
Log.v(LOG_TAG, "startForegroundCompat() called");
// If we have the new startForeground API, then use it.
if (mStartForeground != null) {
mStartForegroundArgs[0] = Integer.valueOf(id);
mStartForegroundArgs[1] = notification;
try {
mStartForeground.invoke(this, mStartForegroundArgs);
} catch (InvocationTargetException e) {
// Should not happen.
Log.w(LOG_TAG, "Unable to invoke startForeground", e);
} catch (IllegalAccessException e) {
// Should not happen.
Log.w(LOG_TAG, "Unable to invoke startForeground", e);
}
return;
}
/*
// Fall back on the old API.
setForeground(true);
mNM.notify(id, notification);
*/
}
/**
* This is a wrapper around the new stopForeground method, using the older
* APIs if it is not available.
*/
private void stopForegroundCompat(int id) {
Log.v(LOG_TAG, "stopForegroundCompat() called");
// If we have the new stopForeground API, then use it.
if (mStopForeground != null) {
mStopForegroundArgs[0] = Boolean.TRUE;
try {
mStopForeground.invoke(this, mStopForegroundArgs);
} catch (InvocationTargetException e) {
// Should not happen.
Log.w(LOG_TAG, "Unable to invoke stopForeground", e);
} catch (IllegalAccessException e) {
// Should not happen.
Log.w(LOG_TAG, "Unable to invoke stopForeground", e);
}
return;
}
/*
// Fall back on the old API. Note to cancel BEFORE changing the
// foreground state, since we could be killed at that point.
mNM.cancel(id);
setForeground(false);
*/
}
private boolean startMockLocation() {
Log.v(LOG_TAG, "startMockLocation() called");
LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mMockGpsProvider = new MockGpsProvider(MockGpsService.this);
if (!mLocationManager.isProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER)) {
// otherwise enable the mock GPS provider
mLocationManager.addTestProvider(MockGpsProvider.GPS_MOCK_PROVIDER, false, false,
false, false, true, false, false, 0, 5);
mLocationManager.setTestProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER, true);
}
if (!mLocationManager.isProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER)) {
return false;
}
try {
mMockGpsProvider.requestMockLocationUpdates();
} catch (IOException e) {
Log.w(LOG_TAG, e);
return false;
}
return true;
}
private void stopMockLocation() {
Log.v(LOG_TAG, "stopMockLocation() called");
LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mMockGpsProvider.removeMockLocationUpdates();
try {
mLocationManager.setTestProviderEnabled("gps", false);
mLocationManager.removeTestProvider(MockGpsProvider.GPS_MOCK_PROVIDER);
} catch (Exception e){
Log.w(LOG_TAG, e);
}
}
@Override
public void onDestroy() {
Log.v(LOG_TAG, "onDestroy() called");
stopMockLocation();
// Make sure our notification is gone.
stopForegroundCompat(NOTIFICATION_ID);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)
支持(0) 盖楼(回复)