【发布时间】:2016-03-31 17:12:03
【问题描述】:
我似乎无法弄清楚这一点。我有一个在清单中注册的广播接收器。我每分钟 ping 一次 gps,broadcastReceiver 的 onReceive 触发并启动服务。该服务获取唤醒锁以防万一,使用 ASyncTask 将 GPS 坐标发送到我们的服务器并释放唤醒锁,然后调用 stopSelf()。这会在我的 Nexus 6p 和 HTC 上持续触发。
但是在三星 GS5 上,这只能持续很长时间。它停止的时间似乎是随机的,但通常在 30 分钟内,有时短至 5 分钟。 broadcastReceiver 再也不会被调用,这意味着 onReceive 只是停止触发。
我注意到三星上的所有省电设置都已关闭。除非有一个超级棘手的隐藏,否则我无法弄清楚三星手机如何停止这个广播接收器,或者停止 GPS,无论发生什么。
即使应用程序未滑动关闭,也会发生这种情况。手机闲置,屏幕关闭,大约 5-30 分钟后,手机停止获取坐标。
无论我使用 GPS_PROVIDER 还是 NETWORK_PROVIDER,都会发生这种情况,尽管使用网络提供商它似乎发生得更快。
这是我启动 GPS 的地方。
public void startBackgroundGPS () {
Activity activity = this.cordova.getActivity();
Context context = activity.getApplicationContext();
ComponentName component = new ComponentName(context, LocationReceiver.class);
int status = context.getPackageManager().getComponentEnabledSetting(component);
Log.d(TAG, Integer.toString(status));
if(status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED || status == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
Log.d(TAG, "receiver is enabled");
//getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
} else if(status == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
Log.d(TAG, "receiver is disabled");
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED , PackageManager.DONT_KILL_APP);
}
Intent intent = new Intent(context, LocationReceiver.class);
intent.setAction("myBroadcast");
intent.putExtra("session_id", session_id);
intent.putExtra("device_id", device_id);
//intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
PendingIntent pendingIntent = PendingIntent.getBroadcast(activity.getApplicationContext(), 58534, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//Register for broadcast intents
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, pendingIntent);
}
这是我的广播接收器
public class LocationReceiver extends BroadcastReceiver {
//private static boolean semaphore = true;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("DistanceFilterLocationService", intent.getAction());
Location location = (Location) intent.getExtras().get(android.location.LocationManager.KEY_LOCATION_CHANGED);
Intent locationServiceIntent = new Intent(context, DistanceFilterLocationService.class);
locationServiceIntent.putExtra("device_id", intent.getStringExtra("device_id"));
locationServiceIntent.putExtra("session_id", intent.getStringExtra("session_id"));
Double longi = location.getLongitude();
Double lati = location.getLatitude();
locationServiceIntent.putExtra("longitude", longi);
locationServiceIntent.putExtra("latitude", lati);
locationServiceIntent.putExtra("accuracy", location.getAccuracy());
locationServiceIntent.putExtra("time", location.getTime());
context.startService(locationServiceIntent);
}
}
这是清单中的广播接收器。
<receiver android:name="com.mypackage.LocationReceiver">
<intent-filter>
<action android:name="myBroadcast" />
</intent-filter>
</receiver>
有人遇到过这样的事情吗?发现了几个类似的问题,但没有给出答案。这让我很生气,因为它完全破坏了三星手机上应用程序的用途。
感谢您的帮助。
【问题讨论】:
标签: android gps samsung-mobile