仅在活动完成时播放成功声音

时间:2018-07-19 06:08:21

标签: java android

我创建了一个可以与他们共享图像的应用程序。共享过程完成后,我设置了通知声音。但是,如果我按下“取消/返回”按钮,声音仍会播放。 在下面的代码中,我设置了一个共享按钮,一旦单击它,就可以通过android的基本共享应用程序共享图像的位图。 共享完成后,如何将声音设置为仅播放。 谢谢, 萨菲

这是我的代码:

shareBtn = findViewById(R.id.shareButton);
    shareBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.startAnimation(animTranslate);
            shareDigitalCard();
        }
    });

private void shareDigitalCard() {

    if (ActivityCompat.checkSelfPermission(MyMenu.this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        try {
            makeStorageRequest();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }else {
        Bitmap viewBmp = Bitmap.createBitmap(mPager.getWidth(), 
        mPager.getHeight(), Bitmap.Config.ARGB_8888);
       viewBmp.setDensity(mPager.getResources().getDisplayMetrics().densityDpi);
        Canvas canvas = new Canvas(viewBmp);
        //mPager.layout(0, 0 , mPager.getLayoutParams().width, mPager.getLayoutParams().height);
        mPager.draw(canvas);

        path = String.valueOf(saveTempBitmap(viewBmp));

        shareImage(path);
    }
}

private void shareImage(String file) {
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

    Intent share = new Intent(Intent.ACTION_SEND);

    // If you want to share a png image only, you can do:
    // setType("image/png"); OR for jpeg: setType("image/jpeg");
    share.setType("image/*");

    File imageFileToShare = new File(file);

    Uri uri = Uri.fromFile(imageFileToShare);
    share.putExtra(Intent.EXTRA_STREAM, uri);

    try {
        startActivityForResult(Intent.createChooser(share, "Share Image!"),123);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 123){
        if (isSoundEnable) {
            MediaPlayer player = null;
            if (player == null) {
                player = MediaPlayer.create(this, R.raw.success);
                final MediaPlayer finalPlayer = player;
                player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        stopPlayer(finalPlayer);
                    }
                });
            }
            player.start();
        }

    } else {
        Toast.makeText(this, "An Error Has Occured !", Toast.LENGTH_SHORT).show();
    }
}

1 个答案:

答案 0 :(得分:0)

您无法获得此事件。共享过程取决于另一个应用程序,您没有任何控制权或结果。您所知道的只是您已经向一个特定的应用程序发送了一个Intent,但是它的作用完全取决于它。