未经READ_SMS权限的android短信验证 [英] android sms verification without READ_SMS permission

查看:332
本文介绍了未经READ_SMS权限的android短信验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,使用Android O,我们现在可以读取SMS验证,而无需READ_SMS权限.可以使用createAppSpecificSmsToken API来完成.

但是我需要一个完整的示例来演示整个SMS验证例程.

I know that with Android O, now we can read SMS verification without requiring READ_SMS permission. It could be done using createAppSpecificSmsToken API.

But I need a complete example to demonstrate whole of SMS verification routine.

推荐答案

没有太多内容.调用SmsManager上的createAppSpecificSmsToken(),提供PendingIntent.您会得到String返回,这是令牌.如果设备收到带有该令牌的SMS,则您的PendingIntent运行,并触发您指定的任何组件.

There is not much to it. Call createAppSpecificSmsToken() on SmsManager, supplying a PendingIntent. You get a String back which is the token. If the device receives an SMS with that token, your PendingIntent is run, triggering whatever component you specified.

/***
  Copyright (c) 2017 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  Covered in detail in the book _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
 */

package com.commonsware.android.sms.token;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.TextView;

public class MainActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SmsManager mgr=SmsManager.getDefault();
    String token=mgr.createAppSpecificSmsToken(buildPendingIntent());
    TextView tv=(TextView)findViewById(R.id.text);

    tv.setText(getString(R.string.msg, token));
  }

  private PendingIntent buildPendingIntent() {
    return(PendingIntent.getActivity(this, 1337,
      new Intent(this, ResultActivity.class), 0));
  }
}

在这里,我将令牌显示在TextView中,因此您可以将其键入到其他设备上的SMS客户端中,并将令牌绑定到ResultActivity.

Here, I display the token in a TextView, so you can type it into an SMS client on some other device, and tie the token to a ResultActivity.

您指定的组件(例如ResultActivity)在其附加组件中接收实际的SMS消息,您可以使用Telephony.Sms.Intents.getMessagesFromIntent()来获取它:

Your designated component (e.g., ResultActivity) receives the actual SMS message in its extras, and you can use Telephony.Sms.Intents.getMessagesFromIntent() to get at it:

/***
  Copyright (c) 2017 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  Covered in detail in the book _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
 */

package com.commonsware.android.sms.token;

import android.app.Activity;
import android.app.PendingIntent;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.TextView;

public class ResultActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView tv=(TextView)findViewById(R.id.text);

    for (SmsMessage pdu :
      Telephony.Sms.Intents.getMessagesFromIntent(getIntent())) {
      tv.append(pdu.getDisplayMessageBody());
    }
  }
}

这篇关于未经READ_SMS权限的android短信验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆