Android三星手机阻止广播接收者随机获取更新

时间:2016-03-31 17:12:04

标签: android gps samsung-mobile

我似乎无法想出这个。我有一个在清单中注册的broadcastReceiver。我每分钟ping一次gps,broadcastReceiver的onReceive会启动并启动服务。此服务为了以防万一,抓取唤醒锁,使用ASyncTask将GPS Coords发送到我们的服务器并释放唤醒锁,然后调用stopSelf()。这在我的Nexus 6p和HTC上持续发射。

然而,在三星GS5上,这只能使用很长时间。它停止的时间似乎是随机的,但通常在30分钟内,有时短至5分钟。从不会再次调用broadcastReceiver,这意味着onReceive会停止触发。

我注意到三星上的所有省电设置都已关闭。除非有一个超级棘手的隐藏的,我无法弄清楚三星手机如何能够阻止这个广播接收器,或停止GPS,无论发生哪种情况。

即使应用程序未被关闭,也会发生这种情况。手机闲置,屏幕关闭,约5-30分钟后,手机停止接通电源线。

无论我使用GPS_PROVIDER还是NETWORK_PROVIDER,都会发生这种情况,尽管网络提供商似乎更快。

这是我启动GPS的地方。

override func viewDidAppear() {
  let ref = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com")

  ref.observeAuthEventWithBlock({ authData in
    if authData != nil {
        // user authenticated
        print(authData)
        self.performSegueWithIdentifier("LoginToOtherView", sender: nil)
    } else {
        // No user is signed in
    }
  })
}

这是我的broadcastReceiver

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);
}
}

任何人遇到这样的事情?发现了几个类似的问题,但没有给出答案。这个杀了我,因为它完全摧毁了三星手机上的应用程序的目的。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

也许这不是一个好的答案,但也许Android版本可能是问题。或者,也许是三星的一些插件......你是否在其他三星手机上测试过(可能是其他Android版本)?