如何在 android API 25 (Nougat) 中接收短信

How to receive sms in android API 25 (Nougat)

我一直在使用这些代码在实际设备 android 4.2 中接收短信并且它工作正常,现在我开始了解新的权限模块所以这个代码在 android 7 牛轧糖中没有为我工作。

public class sms extends BroadcastReceiver {
  public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    if (bundle != null) {
        Object[] smsExtra = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[smsExtra.length];

        for (int i = 0; i < msgs.length; i++) {
            SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
            String body = sms.getMessageBody().toString();
            String sender = sms.getOriginatingAddress().toString();
            Toast.makeText(context, "From :"+sender+"\n"+"body:"+body, Toast.LENGTH_LONG).show();


        }
    }
}

}

我发现 this 关于 marshmallow 需要 运行 时间许可,但我不知道如何以及在我的代码中添加它的位置,以便它在 Nougat 及以下版本中工作apis.

// 在 manifest.xml

  <uses-permission android:name="android.permission.RECEIVE_SMS" />

试试这个来读取短信权限运行时

requestSmsPermission();

private void requestSmsPermission() {
    String permission = Manifest.permission.READ_SMS;
    int grant = ContextCompat.checkSelfPermission(this, permission);
    if (grant != PackageManager.PERMISSION_GRANTED) {
        String[] permission_list = new String[1];
        permission_list[0] = permission;
        ActivityCompat.requestPermissions(this, permission_list, 1);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 1) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                                            Toast.makeText(AccountClass.this,"permission granted", Toast.LENGTH_SHORT).show();


        } else {
            Toast.makeText(AccountClass.this,"permission not granted", Toast.LENGTH_SHORT).show();
        }
    }

}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.messagebox);


    if (!CheckPermission(MessageBox.this, Manifest.permission.READ_SMS)) {

        RequestPermission(MessageBox.this, Manifest.permission.READ_SMS, REQUEST_RUNTIME_PERMISSION);

    }
}

@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {

    switch (permsRequestCode) {

        case REQUEST_RUNTIME_PERMISSION: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // you have permission go ahead
                //createApplicationFolder();
            } else {
                // you do not have permission show toast.
            }
            return;
        }
    }
}

public void RequestPermission(Activity thisActivity, String Permission, int Code) {
    if (ContextCompat.checkSelfPermission(thisActivity, Permission) != PackageManager.PERMISSION_GRANTED)
    {
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Permission))
        { }
        else
            {
            ActivityCompat.requestPermissions(thisActivity, new String[]{Permission}, Code);
        }
    }
}

public boolean CheckPermission(Context context, String Permission) {
    if (ContextCompat.checkSelfPermission(context, Permission) == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;
    }
}