android 未经 READ_SMS 许可的短信验证
android sms verification without READ_SMS permission
我知道有了Android O,现在我们可以读取短信验证而不需要READ_SMS权限。可以使用 createAppSpecificSmsToken API.
来完成,但我需要一个完整的示例来演示整个 SMS 验证例程。
没有太多内容。在 SmsManager
上调用 createAppSpecificSmsToken()
,提供 PendingIntent
。你得到一个 String
回来,这是令牌。如果设备收到带有该令牌的短信,则您的 PendingIntent
是 运行,触发您指定的任何组件。
/***
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
。
您指定的组件(例如,ResultActivity
)在其 extras 中接收实际的 SMS 消息,您可以使用 Telephony.Sms.Intents.getMessagesFromIntent()
获取它:
/***
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());
}
}
}
我知道有了Android O,现在我们可以读取短信验证而不需要READ_SMS权限。可以使用 createAppSpecificSmsToken API.
来完成,但我需要一个完整的示例来演示整个 SMS 验证例程。
没有太多内容。在 SmsManager
上调用 createAppSpecificSmsToken()
,提供 PendingIntent
。你得到一个 String
回来,这是令牌。如果设备收到带有该令牌的短信,则您的 PendingIntent
是 运行,触发您指定的任何组件。
/***
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
。
您指定的组件(例如,ResultActivity
)在其 extras 中接收实际的 SMS 消息,您可以使用 Telephony.Sms.Intents.getMessagesFromIntent()
获取它:
/***
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());
}
}
}