活动未收到的广播意图

时间:2015-07-19 21:49:01

标签: android android-intent broadcastreceiver

我正在尝试在2个活动之间广播Intent。

Intent从FilterActivity发送到PreviewActivity,如下所示

mFiltersView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                CameraFiltersFactory effect = (CameraFiltersFactory) v.findViewById(R.id.filteredImage).getTag();
                final Intent previewIntent = new Intent(FiltersActivity.this, PreviewActivity.class);    
                startActivityForResult(previewIntent, 0);

                effect.save(new GPUImage.OnPictureSavedListener() {

                    @Override public void onPictureSaved(Uri uri) {    
                        final Intent ImageIntent = new Intent();
                        ImageIntent.setData(uri);     
                        ImageIntent.setAction("com.test.FILTER_RENDERED");
                        sendBroadcast(ImageIntent);
                    }
                });

在PreviewActivity中,我需要注册接收器并使用IntentFiter来捕获Intent并检查它。

public class PreviewActivity extends Activity {

    ReceiveMessages myReceiver = null;
    Boolean myReceiverIsRegistered = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myReceiver = new ReceiveMessages();
        if (!myReceiverIsRegistered) {
            // registerReceiver(myReceiver, new IntentFilter());
            registerReceiver(myReceiver, new IntentFilter("com.test.FILTER_RENDERED"));

            myReceiverIsRegistered = true;
        }
     ....

    @Override
    protected void onResume(){
        super.onResume();
        if (!myReceiverIsRegistered) {
            registerReceiver(myReceiver, new IntentFilter("com.test.FILTER_RENDERED"));
            myReceiverIsRegistered = true;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (myReceiverIsRegistered) {
            unregisterReceiver(myReceiver);
            myReceiverIsRegistered = false;
        }
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Intent mNewIntent = intent;

        Bundle bundle = null;
        String fragment = null;
        if (intent != null) {
            mImagePreviewUri = mNewIntent.getData();
            if(mImagePreviewUri!=null) {
                mCameraActionsView.setEnabled(true);
                mImagePreview.setImageURI(mImagePreviewUri);
            }
        }
    }

    public class ReceiveMessages extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
             if(action.equalsIgnoreCase("com.test.FILTER_RENDERED")){
                if (intent != null) {
                    mImagePreviewUri = intent.getData();
                    if(mImagePreviewUri!=null) {
                        mCameraActionsView.setEnabled(true);
                        mImagePreview.setImageURI(mImagePreviewUri);
                    }
                }
            }
        }
    }

}

不幸的是,活动&lt;

从未收到过Intent

知道为什么,OnReceive没有被触发?

1 个答案:

答案 0 :(得分:0)

当广播从您的FilterActivity发送到PreviewActivity时,预览活动中的BroadcastReceiver尚未注册接收任何广播,因此它会错过它。普通广播需要在广播时注册接收器以便接收它。它不会留下来等待你的接收器注册。在你的代码中,接收器只有在你的PreviewActivity进入前景后才能注册,即在OnResume方法中,到那时广播已经消失了。此外,您的接收器在onStop中未注册,即一旦PreviewActivity进入后台。因此,当预览活动不在前台时,所有后续广播都将被丢失,直到再次调用onResume。如果您使用StickyBroadcast而不是普通广播,则接收方可以播放和接收广播。但StickyBroadcasts存在安全隐患,因为它的使用已被弃用。