如何使用 LocalBroadcastManager 调用不同的方法

How to call different methods with LocalBroadcastManager

我的操作栏上有 3 个菜单按钮(add_imagesave_memeshare_meme),它们在 Mainactivity.java 中声明。按钮负责调用另一个片段中的相应方法。使用 LocalBroadcastManager 建立连接。

public class MainActivity extends AppCompatActivity{
    Intent saveIntent, shareIntent, addImageIntent;
    LocalBroadcastManager localBroadcastManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        localBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());

        addImageIntent = new Intent("ADD_ACTION");
        saveIntent = new Intent("SAVE_ACTION");
        shareIntent = new Intent("SHARE_ACTION");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.add_image:
                localBroadcastManager.sendBroadcast(addImageIntent);
            case R.id.save_meme:
                localBroadcastManager.sendBroadcast(saveIntent);
            case R.id.share_meme:
                localBroadcastManager.sendBroadcast(shareIntent);
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

上面提到了对应的方法在TopImageFragment.javaclass中。 add_image 按钮调用允许用户将图像从设备上传到 ImageView 的方法:

public void selectedImage() {
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(intent, PICK_IMAGE);
}

save_meme按钮调用保存位图到设备的方法:

public void saveMeme(Bitmap btm) {
        counter++;

        File file;
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath();
        file = new File(path + "/Memery/" + timeStamp + counter + ".jpg");
        file.getParentFile().mkdir();

        try {
            OutputStream stream = new FileOutputStream(file);
            btm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            stream.flush();
            stream.close();
            Toast.makeText(getContext(), "Meme Saved", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Uri contentUri = Uri.fromFile(file);
        mediaScanIntent.setData(contentUri);
        Objects.requireNonNull(getContext()).sendBroadcast(mediaScanIntent);
    }

share_meme 按钮调用允许用户在各种社交媒体应用程序和电子邮件客户端上分享模因的方法:

public void shareMeme(Bitmap bitmap) {
    String path = MediaStore.Images.Media.insertImage(Objects.requireNonNull(getContext()).getContentResolver(), bitmap, "Meme", null);
    Uri uri = Uri.parse(path);

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    share.putExtra(Intent.EXTRA_STREAM, uri);
    share.putExtra(Intent.EXTRA_TEXT, "This is my Meme");
    getContext().startActivity(Intent.createChooser(share, "Share Your Meme!"));
}

但现在发生的情况是,每当我 select 3 个菜单按钮中的任何一个时,都会调用所有 3 个方法。这是我的完整片段:

public class TopImageFragment extends Fragment {

    ImageView imageView;
    TextView topTextView, bottomTextView;
    RelativeLayout topImageRelativeLayout;
    public static final int PICK_IMAGE = 1;
    Uri imageUri;
    int counter = 0;
    String timeStamp = new SimpleDateFormat("yyyyMMdd", Locale.CANADA).format(new Date());
    float textSize, x, imageViewArea, canvasArea;
    double val;


    MemeViewModel memeViewModel;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_top_image, container, false);

        imageView = view.findViewById(R.id.meme_image_view);

        topTextView = view.findViewById(R.id.top_text_view);
        bottomTextView = view.findViewById(R.id.bottom_text_view);

        topImageRelativeLayout = view.findViewById(R.id.top_image_relative_layout);

        if (savedInstanceState != null) {
            imageUri = savedInstanceState.getParcelable("imageUri");
            imageView.setImageURI(imageUri);
        }
        return view;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable("imageUri", imageUri);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        memeViewModel = ViewModelProviders.of(Objects.requireNonNull(getActivity())).get(MemeViewModel.class);

        memeViewModel.getTopText().observe(getViewLifecycleOwner(), new Observer<CharSequence>() {
            @Override
            public void onChanged(@Nullable CharSequence charSequence) {
                topTextView.setText(charSequence);
            }
        });

        memeViewModel.getBottomText().observe(getViewLifecycleOwner(), new Observer<CharSequence>() {
            @Override
            public void onChanged(@Nullable CharSequence charSequence) {
                bottomTextView.setText(charSequence);
            }
        });
    }

    public void selectedImage() {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(intent, PICK_IMAGE);
    }

    public Bitmap getBitmap(ImageView img) {
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable());
        Bitmap bitmap = bitmapDrawable.getBitmap();

        return bitmap.copy(Bitmap.Config.ARGB_8888, true);
    }

    public Bitmap screenshotMeme() {
        Bitmap image = Bitmap.createBitmap(topImageRelativeLayout.getWidth(), topImageRelativeLayout.getHeight(), Bitmap.Config.RGB_565);
        topImageRelativeLayout.draw(new Canvas(image));

        return image.copy(Bitmap.Config.ARGB_8888, true);
    }

    public Bitmap drawMeme(Bitmap mutableBitmap) {

        String topText = topTextView.getText().toString();
        String bottomText = bottomTextView.getText().toString();

        topText = topText.toUpperCase();
        bottomText = bottomText.toUpperCase();

        Canvas canvas = new Canvas(mutableBitmap);

        TextPaint topFillPaint = new TextPaint();
        TextPaint bottomFillPaint = new TextPaint();

        TextPaint topStrokePaint = new TextPaint();
        TextPaint bottomStrokePaint = new TextPaint();

        Typeface typeface = getResources().getFont(R.font.impact);

        textSize = topTextView.getTextSize();
        imageViewArea = (imageView.getWidth()) * (imageView.getHeight());
        canvasArea = (canvas.getWidth()) * (canvas.getHeight());
        val = textSize * sqrt(canvasArea / imageViewArea);
        x = (float) val;

        topFillPaint.setColor(Color.WHITE);
        topFillPaint.setTextSize(x);
        topFillPaint.setTypeface(typeface);
        topFillPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        topStrokePaint.setStyle(Paint.Style.STROKE);
        topStrokePaint.setStrokeWidth(4);
        topStrokePaint.setTextSize(x);
        topStrokePaint.setColor(Color.BLACK);
        topStrokePaint.setTypeface(typeface);

        bottomFillPaint.setColor(Color.WHITE);
        bottomFillPaint.setTextSize(x);
        bottomFillPaint.setTypeface(typeface);
        bottomFillPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        bottomStrokePaint.setStyle(Paint.Style.STROKE);
        bottomStrokePaint.setStrokeWidth(4);
        bottomStrokePaint.setColor(Color.BLACK);
        bottomStrokePaint.setTextSize(x);
        bottomStrokePaint.setTypeface(typeface);

        StaticLayout topFillLayout = new StaticLayout(topText, topFillPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                0.8f, 0.0f, false);
        StaticLayout topStrokeLayout = new StaticLayout(topText, topStrokePaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                0.8f, 0.0f, false);
        StaticLayout bottomFillLayout = new StaticLayout(bottomText, bottomFillPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                0.8f, 0.0f, false);
        StaticLayout bottomStrokeLayout = new StaticLayout(bottomText, bottomStrokePaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                0.8f, 0.0f, false);

        topFillLayout.draw(canvas);

        topStrokeLayout.draw(canvas);

        canvas.translate(0, canvas.getHeight() - bottomFillLayout.getHeight());
        bottomFillLayout.draw(canvas);

        bottomStrokeLayout.draw(canvas);

        return mutableBitmap;
    }

    public void saveMeme(Bitmap btm) {
        counter++;

        File file;
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath();
        file = new File(path + "/Memery/" + timeStamp + counter + ".jpg");
        file.getParentFile().mkdir();

        try {
            OutputStream stream = new FileOutputStream(file);
            btm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            stream.flush();
            stream.close();
            Toast.makeText(getContext(), "Meme Saved", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Uri contentUri = Uri.fromFile(file);
        mediaScanIntent.setData(contentUri);
        Objects.requireNonNull(getContext()).sendBroadcast(mediaScanIntent);
    }

    public void shareMeme(Bitmap bitmap) {
        String path = MediaStore.Images.Media.insertImage(Objects.requireNonNull(getContext()).getContentResolver(), bitmap, "Meme", null);
        Uri uri = Uri.parse(path);

        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/*");
        share.putExtra(Intent.EXTRA_STREAM, uri);
        share.putExtra(Intent.EXTRA_TEXT, "This is my Meme");
        getContext().startActivity(Intent.createChooser(share, "Share Your Meme!"));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
            imageUri = Objects.requireNonNull(data).getData();
            imageView.setImageURI(imageUri);
            memeViewModel.setSharedId(imageUri.getLastPathSegment());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(Objects.requireNonNull(getActivity())).registerReceiver(addListener, new IntentFilter("ADD_ACTION"));
        LocalBroadcastManager.getInstance(Objects.requireNonNull(getActivity())).registerReceiver(saveListener, new IntentFilter("SAVE_ACTION"));
        LocalBroadcastManager.getInstance(Objects.requireNonNull(getActivity())).registerReceiver(shareListener, new IntentFilter("SHARE_ACTION"));
    }

    private BroadcastReceiver addListener = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            selectedImage();
        }
    };

    private BroadcastReceiver saveListener = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            float height = getBitmap(imageView).getHeight();
            float width = getBitmap(imageView).getWidth();

            if ((height <= 600) || (width <= 600)) {
                saveMeme(screenshotMeme());
                Toast.makeText(getContext(), "screenshotMeme() method was called", Toast.LENGTH_SHORT).show();
            } else {
                saveMeme(drawMeme(getBitmap(imageView)));
                Toast.makeText(getContext(), "drawMeme() method was called", Toast.LENGTH_SHORT).show();
            }
        }
    };

    private BroadcastReceiver shareListener = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            float height = getBitmap(imageView).getHeight();
            float width = getBitmap(imageView).getWidth();

            if ((height <= 600) || (width <= 600)) {
                shareMeme(screenshotMeme());
                Toast.makeText(getContext(), "screenshotMeme() method was called", Toast.LENGTH_SHORT).show();
            } else {
                shareMeme(drawMeme(getBitmap(imageView)));
                Toast.makeText(getContext(), "drawMeme() method was called", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    public void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(Objects.requireNonNull(getActivity())).unregisterReceiver(addListener);
        LocalBroadcastManager.getInstance(Objects.requireNonNull(getActivity())).unregisterReceiver(saveListener);
        LocalBroadcastManager.getInstance(Objects.requireNonNull(getActivity())).unregisterReceiver(shareListener);
    }
}

问题:因为在switch-case块中,你没有return或者放break语句,那么每当一个菜单项被点击时,它会执行所有switch-case 中的语句。

解决方案:在每个 case 上放置一个 return true 以指示您希望在单击菜单项时消耗事件。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.add_image:
            localBroadcastManager.sendBroadcast(addImageIntent);
            return true;
        case R.id.save_meme:
            localBroadcastManager.sendBroadcast(saveIntent);
            return true;
        case R.id.share_meme:
            localBroadcastManager.sendBroadcast(shareIntent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

或对每个案例使用 break 语句,然后 return 在方法结束时为真。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.add_image:
            localBroadcastManager.sendBroadcast(addImageIntent);
            break;
        case R.id.save_meme:
            localBroadcastManager.sendBroadcast(saveIntent);
            break;
        case R.id.share_meme:
            localBroadcastManager.sendBroadcast(shareIntent);
            break;
        default:
            return super.onOptionsItemSelected(item);
    }

    return true;
}