在我的应用程序中,我试图从短信中读取otp而不在我的应用程序中输入内容。 但它无法正常工作我无法找出错误我试图纠正错误超过一周,请帮我找出错误的位置,我创建一个类来读取传入的消息并将值传递给我的OTPActivity页面,但到达那里的空值
IncomingSms.java
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody().replaceAll("\\D", "");
//message = message.substring(0, message.length()-1);
Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
Intent myIntent = new Intent("otp");
myIntent.putExtra("message", message);
myIntent.putExtra("number", senderNum);
LocalBroadcastManager.getInstance(context).sendBroadcast(myIntent);
// Show Alert
}
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
OTPActivity
@Override
public void onResume() {
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("otp"));
super.onResume();
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase("otp")) {
final String message = intent.getStringExtra("message");
// message is the fetching OTP
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
mPinEditText.setText(message);
}
}
};
//Initialization Varibles
private void initViews() {
if ((ActivityCompat.checkSelfPermission(OTPActivity.this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions(OTPActivity.this, new String[]{Manifest.permission.READ_SMS}, 100);
} else {
//Permission Granted
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 100:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Permission Granted
} else {
//Permission Not Granted
Toast.makeText(getApplicationContext(), "Permission not granted", Toast.LENGTH_SHORT).show();
}
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypkg.appanme">
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<receiver android:name=".IncomingSms">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
日志显示此错误
Permission Denial: receiving Intent { act=android.provider.Telephony.SMS_RECEIVED flg=0x19000010 (has extras) } to com.gameloft.android.HEP.GloftM5HP/.iab.SmsReceiver requires android.permission.RECEIVE_SMS due to sender com.android.phone (uid 1001)
答案 0 :(得分:0)
您需要在android:permission="android.permission.BROADCAST_SMS"
中的接收器块中添加AndroidManifest.xml
,这是接收短信所必需的。像这样:
<receiver
android:name=".IncomingSms"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
如果您不想编写接收器,则可以使用简单的轻量级库https://github.com/VitaliBov/SmsInterceptor
您只需要重写接口方法,创建一个Interceptor类并将其与生命周期绑定。看起来像这样:
public class AuthActivity extends AppCompatActivity implements OnMessageListener {
private SmsInterceptor smsInterceptor;
private EditText etAuthPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);
initViews();
initSmsInterceptor();
}
@Override
protected void onResume() {
super.onResume();
smsInterceptor.register();
}
@Override
protected void onPause() {
super.onPause();
smsInterceptor.unregister();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
smsInterceptor.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void messageReceived(String message) {
// You can perform your validation here
etAuthPassword.setText(message);
}
private void initViews() {
etAuthPassword = findViewById(R.id.etAuthPassword);
etAuthPassword.addTextChangedListener(new SmsTextWatcher() {
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() == 4) {
btnAuthPassword.setEnabled(true);
checkCode();
} else {
btnAuthPassword.setEnabled(false);
}
}
});
}
private void initSmsInterceptor() {
smsInterceptor = new SmsInterceptor(this, this);
// Not necessary
smsInterceptor.setRegex(SMS_CODE_REGEX);
smsInterceptor.setPhoneNumber(PHONE_NUMBER);
}
private void checkCode() {
// Validation
if (isValid) {
navigateToMainScreen();
}
}
}