创建多个 pendingIntent
Create multiple pendingIntent
myRange 的值始终为 1、2 或 3。
它给出了空指针异常 error.pending 必须指定。
如果我不检查 if 语句中的 myRange 值,它不会给出错误,但不会创建 pendingIntent2 和 pendingIntent3。
我尝试发送不同的请求代码,但没有成功。
private PendingIntent createGeofencePendingIntent(int myRange) {
Log.d(TAG, "createGeofencePendingIntent");
Toast.makeText(getContext(),"creating intent function" + myRange ,Toast.LENGTH_SHORT).show();
if ( geoFencePendingIntent1 != null && myRange == 1)
return geoFencePendingIntent1;
if ( geoFencePendingIntent2 != null && myRange == 2 )
return geoFencePendingIntent2;
if ( geoFencePendingIntent3 != null && myRange == 3)
return geoFencePendingIntent3;
if(myRange == 1)
{
Toast.makeText(getContext(),"creating intent 1",Toast.LENGTH_SHORT).show();
Intent intent1 = new Intent( getContext(), GeofenceTransitionService.class);
intent1.putExtra("region",inString[0]);
geoFencePendingIntent1 = PendingIntent.getService(
getContext(), GEOFENCE_REQ_CODE, intent1, PendingIntent.FLAG_UPDATE_CURRENT );
return geoFencePendingIntent1;
}
else if (myRange ==2)
{
Toast.makeText(getContext(),"creating intent 2",Toast.LENGTH_SHORT).show();
Intent intent2 = new Intent( getContext(), GeofenceTransitionService.class);
intent2.putExtra("region",inString[1]);
geoFencePendingIntent2 = PendingIntent.getService(
getContext(), 5, intent2, PendingIntent.FLAG_NO_CREATE );
return geoFencePendingIntent2;
}
else if (myRange == 3)
{
Intent intent3 = new Intent( getContext(), GeofenceTransitionService.class);
return PendingIntent.getService(
getContext(), GEOFENCE_REQ_CODE, intent3, PendingIntent.FLAG_UPDATE_CURRENT );
}
geoRange++;
// Toast.makeText(getContext(), "leaving my geofence", Toast.LENGTH_SHORT).show();
return null;
}
你这里有几个问题。第一个是这样的:
geoFencePendingIntent2 = PendingIntent.getService(
getContext(), 5, intent2, PendingIntent.FLAG_NO_CREATE );
这可能总是 return null
正如您指定的那样 FLAG_NO_CREATE
。如果匹配的 PendingIntent
已经存在(它可能不存在),这只会 return 一个非 null
结果。请改用 FLAG_UPDATE_CURRENT
。
第二个问题是您需要确保 3 个不同的 PendingIntent
中的每一个都是唯一的。为此,您需要在对 PendingIntent.getService()
的调用中提供一个唯一的 requestCode
,或者您需要在传递给 PendingIntent.getService()
的 Intent
中提供一个唯一的 ACTION。否则,当您调用 PendingIntent.getService()
时,您将继续获得相同的 PendingIntent
returned(并且不会创建新的)。
Whosebug 上有大约一百万个关于此的问题,其中大部分都详细解释了 PendingIntent
创建和匹配的工作原理。
myRange 的值始终为 1、2 或 3。
它给出了空指针异常 error.pending 必须指定。
如果我不检查 if 语句中的 myRange 值,它不会给出错误,但不会创建 pendingIntent2 和 pendingIntent3。
我尝试发送不同的请求代码,但没有成功。
private PendingIntent createGeofencePendingIntent(int myRange) {
Log.d(TAG, "createGeofencePendingIntent");
Toast.makeText(getContext(),"creating intent function" + myRange ,Toast.LENGTH_SHORT).show();
if ( geoFencePendingIntent1 != null && myRange == 1)
return geoFencePendingIntent1;
if ( geoFencePendingIntent2 != null && myRange == 2 )
return geoFencePendingIntent2;
if ( geoFencePendingIntent3 != null && myRange == 3)
return geoFencePendingIntent3;
if(myRange == 1)
{
Toast.makeText(getContext(),"creating intent 1",Toast.LENGTH_SHORT).show();
Intent intent1 = new Intent( getContext(), GeofenceTransitionService.class);
intent1.putExtra("region",inString[0]);
geoFencePendingIntent1 = PendingIntent.getService(
getContext(), GEOFENCE_REQ_CODE, intent1, PendingIntent.FLAG_UPDATE_CURRENT );
return geoFencePendingIntent1;
}
else if (myRange ==2)
{
Toast.makeText(getContext(),"creating intent 2",Toast.LENGTH_SHORT).show();
Intent intent2 = new Intent( getContext(), GeofenceTransitionService.class);
intent2.putExtra("region",inString[1]);
geoFencePendingIntent2 = PendingIntent.getService(
getContext(), 5, intent2, PendingIntent.FLAG_NO_CREATE );
return geoFencePendingIntent2;
}
else if (myRange == 3)
{
Intent intent3 = new Intent( getContext(), GeofenceTransitionService.class);
return PendingIntent.getService(
getContext(), GEOFENCE_REQ_CODE, intent3, PendingIntent.FLAG_UPDATE_CURRENT );
}
geoRange++;
// Toast.makeText(getContext(), "leaving my geofence", Toast.LENGTH_SHORT).show();
return null;
}
你这里有几个问题。第一个是这样的:
geoFencePendingIntent2 = PendingIntent.getService(
getContext(), 5, intent2, PendingIntent.FLAG_NO_CREATE );
这可能总是 return null
正如您指定的那样 FLAG_NO_CREATE
。如果匹配的 PendingIntent
已经存在(它可能不存在),这只会 return 一个非 null
结果。请改用 FLAG_UPDATE_CURRENT
。
第二个问题是您需要确保 3 个不同的 PendingIntent
中的每一个都是唯一的。为此,您需要在对 PendingIntent.getService()
的调用中提供一个唯一的 requestCode
,或者您需要在传递给 PendingIntent.getService()
的 Intent
中提供一个唯一的 ACTION。否则,当您调用 PendingIntent.getService()
时,您将继续获得相同的 PendingIntent
returned(并且不会创建新的)。
Whosebug 上有大约一百万个关于此的问题,其中大部分都详细解释了 PendingIntent
创建和匹配的工作原理。