【问题标题】:I was trying to make an app that automatically detects the OTP send via the default Android messaging app我正在尝试制作一个通过默认 Android 消息应用程序自动检测 OTP 发送的应用程序
【发布时间】:2016-11-19 01:57:16
【问题描述】:

这是我的代码:

 btnSend.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        final String sentOTP = RandomNum();
        if (!etNumber.getText().toString().trim().equals("")) {
        Uri uri = Uri.parse("smsto:" + etNumber.getText().toString().trim());
        Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
        smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        smsIntent.putExtra("sms_body", sentOTP);
        startActivity(smsIntent);
        inish();
        Intent i = new Intent(sendActivity.this, CheckerActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("generatedOTP", sentOTP);
        startActivity(i);
        finish();
        Toast.makeText(sendActivity.this, "Enter the phone NUMBER!!", Toast.LENGTH_LONG).show();
        }
   }
});

单击按钮后,消息传递应用程序将打开,并由启动的 CheckerActivity 作为后台。我想要的是,当消息应用程序打开时,我发送消息,并且只要我单击发送按钮发送消息,CheckerActivity 也会启动。

我怎样才能做到这一点?我需要设置一些额外的 Intent 标志吗?

【问题讨论】:

  • 制作发送消息时调用的接收器,并从此广播接收器检查最后发送的消息是否属于您。然后调用您的 CheckerActivity。

标签: android android-intent one-time-password


【解决方案1】:

使用结果启动 SMS Intent,然后您可以根据结果开始您的活动。

【讨论】:

  • @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SMS_REQUEST) { if (resultCode == RESULT_OK) { Intent i = new Intent(sendActivity.this, CheckerActivity.class); i.putExtra("生成的OTP", sentOTP);开始活动(一);结束(); } } } 这行得通吗??
  • 是的,这应该可以解决问题,不要忘记启动您的短信活动,例如..'startActivityForResult(smsIntent, SMS_REQUEST);'
猜你喜欢
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 2010-10-03
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 2016-11-06
  • 2012-07-25
相关资源
最近更新 更多