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.
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));
}
}
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
.
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());
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With