LocationManager.addProximityAlert(...)引发IllegalArgumentException(“挂起的意图必须以包为目标”)

时间:2019-03-08 20:44:58

标签: android

我正在尝试使用LocationManager.addProximityAlert(...)设置地理围栏,但得到IllegalArgumentException("pending intent must be targeted to package")

这是我的代码:

Intent intent = new Intent(context, MyIntentService.class);
intent.setPackage(context.getPackageName());
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

locationManager.addProximityAlert(latitude, longitude, radius, expiration, pendingIntent);

LocationManager要做的第一件事是调用checkPendingIntent(intent),调用intent.isTargetedToPackage(),调用ActivityManagerNative.isIntentSenderTargetedToPackage(IIntentSender sender) https://android.googlesource.com/platform/frameworks/base/+/c80f952/core/java/android/app/ActivityManagerNative.java#4609

结果是false,我得到了IllegalArgumentException("pending intent must be targeted to package")

如果我使用BroadcastReceiver而不是IntentService,同样的事情也可以正常工作。

如何解决此IllegalArgumentException

P.S。我将LocationManager用于未安装Play服务的设备。使用IntentService立即运行一些API请求,这可能需要一些时间。 IntentService在清单中注册为<service android:name=".MyIntentService" android:exported="false"/>

更新:这是与BroadcastReceiver一起使用的代码。 AndroidManifest:

<receiver android:name=".MyBroadcastReceiver" android:exported="false">
    <intent-filter>
        <action android:name="MyBroadcastReceiver.GEOFENCE_TRANSITION"/>
    </intent-filter>
</receiver>

java代码:

Intent intent = new Intent("MyBroadcastReceiver.GEOFENCE_TRANSITION");
intent.setPackage(context.getPackageName());
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

0 个答案:

没有答案