如何在Android中以编程方式按回Whatsapp上的按钮?

时间:2017-04-02 05:20:04

标签: android android-intent android-activity

我制作了社交分享应用程序,用于在Gmail,FB,Twitter,Whatsapp等不同平台上共享图像。

我正在使用Intent进行whatsapp共享。但我需要在分享后关闭Whatsapp。

我正在使用计时器,在计时器失效时我想触发后退按钮,这样我就可以回到父母活动了。

代码:

      public  void sendWhatsapp() {
        final Dialog dialog1 = new Dialog(this);


        Log.d("LVMH", " opening MAIL dialog ");
        dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog1.setContentView(R.layout.whatsapp_layout);
        dialog1.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        dialog1.setCanceledOnTouchOutside(true);
        dialog1.setCancelable(true);

        dialog1.setOnCancelListener(
                new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {

                        dialog1.dismiss();
                        Log.d("LVMH", " cancelled MAIL dialog ");

                    }
                }
        );

        //calling mail send button inside popup
        final Button whatsapp = (Button)dialog1.findViewById(R.id.buttonSend1);
        editTextEmail1 = (EditText) dialog1.findViewById(R.id.editTextMail1);
        editTextName1 = (EditText) dialog1.findViewById(R.id.editTextName1);

        final Button closeButton = (Button) dialog1.findViewById(R.id.btClose);

        closeButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Log.d("LVMH", "close button clicked ");

                //closeButton.setVisibility(GONE);
                dialog1.dismiss();
                sharingScreen();

            }
        });


        whatsapp.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub



                dialog1.dismiss();

                Log.d("LVMH", "mail button clicked ");

                final String email = editTextEmail1.getText().toString().trim();
                final String subject = editTextName1.getText().toString().trim();
                Log.d("lvmh","whatsapp number:"+email);
                Log.d("lvmh","whatsapp name:"+subject);
                addContact(subject,"+91"+email);


                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_TEXT,"hey , "+subject + "\nHere is your image from #IPL #RCB" );
                shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imagePath));
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                shareIntent.setType("image/*");
                shareIntent.setPackage("com.whatsapp");
                startActivity(shareIntent);

              Toast.makeText(getApplicationContext(), "Wait for Few seconds before your contact apperars in List , Your contact will be removed automatically.",
                        Toast.LENGTH_LONG).show();



                int FinishTime = 13;
                int countDownInterval = 1000;
                CountDownTimer   counterTimer = new CountDownTimer(FinishTime * 1000, countDownInterval) {
                    public void onFinish() {
                        //finish your activity here

                        deleteContact(getApplicationContext(),email,subject);
                        Log.d("LVMH", "Whats app finished");
                      //  this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
                      //  this.onBackPressed();
                        //finish();
                    }

                    public void onTick(long millisUntilFinished) {
                        //called every 1 sec coz countDownInterval = 1000 (1 sec)
                        Log.d("LVMH", "Ticks");
                    }
                };
                counterTimer.start();



// we will delete the numbers
                //

            }
        });
        dialog1.show();// opening mail dialog

    }

更新:我在Oncreate(MainActivity)上调用此事件。

我评论了我尝试过的不同代码。没有任何效果。

让我知道任何意见?

由于 NJ

2 个答案:

答案 0 :(得分:0)

  

我正在使用Intent进行whatsapp共享。但我需要在分享后关闭Whatsapp。

您无法控制其他应用的行为方式。你应该等到用户按下whatsapp

退出

答案 1 :(得分:0)

我添加了以下几行。这有助于我回到主要活动。

Intent intent = new Intent(MainActivity. this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);