我的广播接收器是显式的还是隐式的?

时间:2018-06-23 06:06:54

标签: android android-broadcastreceiver

在阅读了有关此的一些手册(12)之后,我仍然需要帮助。我将我的应用程序定位为android O,在android 7.0上运行良好,但在8.1上似乎没有任何广播。因此,如果在清单中定位android O并在7.0上运行并使用隐式广播,它仍然可以正常工作吗? 您能帮我确定我的广播是显式的还是隐式的? 我正在使用Awareness API ...

清单:

   <receiver android:name=".DetectionBroadcastReceiver" >
        <intent-filter>
            <action android:name="childincar.com.michlindevelopment.DETECTIONFENCE" />
        </intent-filter>
    </receiver>

DetctionBroadcastReceiver

public class DetectionBroadcastReceiver extends BroadcastReceiver {

    Context context;

    @Override
    public void onReceive(Context context, Intent intent) {


        Log.d("DTAG", "onReceive");
        this.context = context;

        if (!TextUtils.equals(Constans.FENCE_RECEIVER_ACTION, intent.getAction())) {
            return;
        }

        //Some Code
    }
}

康斯坦斯

public class Constans {
    public static final String FENCE_RECEIVER_ACTION = BuildConfig.APPLICATION_ID + ".DETECTIONFENCE";
}

注册

 public static void registerFences(final Context context) {

        Intent intent = new Intent(Constans.FENCE_RECEIVER_ACTION);
        PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);


        Awareness.getFenceClient(context).updateFences(new FenceUpdateRequest.Builder()
                .addFence(Constans.DETECTION_FENCE_DRIVING, DetectedActivityFence.starting(DetectedActivity.IN_VEHICLE), mPendingIntent)
                .addFence(Constans.DETECTION_FENCE_WALKING, DetectedActivityFence.starting(DetectedActivity.WALKING), mPendingIntent)
                .build())
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                    }
                });
    }

1 个答案:

答案 0 :(得分:1)

任何不特定于您的应用的广播都是隐式的。例如,“ ACTION_MY_PACKAGE_REPLACED”的广播接收器是特定于您的应用的,应该是显式的,而“ ACTION_PACKAGE_REPLACED”是隐式的,因为它会通知您所有软件包。

您的广播接收器似乎是隐含的,因为它不仅是针对“您的”应用程序而设计的。