如何在Android API 25(牛轧糖)中接收短信

时间:2017-06-09 08:53:37

标签: android sms android-7.0-nougat smsmanager android-7.1-nougat

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

function getLocationInfoByIp(){
$client  = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote  = @$_SERVER['REMOTE_ADDR'];
$result  = array('country'=>'', 'city'=>'');
if(filter_var($client, FILTER_VALIDATE_IP)){
    $ip = $client;
}elseif(filter_var($forward, FILTER_VALIDATE_IP)){
    $ip = $forward;
}else{
    $ip = $remote;
}
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));    
if($ip_data && $ip_data->geoplugin_countryName != null){
    $result['country'] = $ip_data->geoplugin_countryCode;
    $result['city'] = $ip_data->geoplugin_city;
}
return $result;

}

我发现this关于棉花糖需要运行时许可,但我不知道如何以及在我的代码中将其添加到哪里以便它在Nougat和apis下工作。

在manifest.xml中

//

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();


        }
    }
}

2 个答案:

答案 0 :(得分:1)

尝试阅读sms permisson runtime

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();
        }
    }

}

答案 1 :(得分:0)

@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;
    }
}