![](/img/trans.png)
[英]java.lang.SecurityException: Sending SMS message: uid 10051 does not have android.permission.SEND_SMS
[英]java.lang.SecurityException: Sending SMS message: uid 10282 does not have android.permission.SEND_SMS
我的應用程序不斷崩潰,出現此錯誤代碼:
java.lang.SecurityException: Sending SMS message: uid 10282 does not have android.permission.SEND_SMS.
這是我的代碼
Button button2 = (Button) findViewById(R.id.text);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String messageToSend = "this is a text";
String number = "XXXXXX";
SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null);
}
});
}
嘗試在您的第一個活動的 onCreate 方法中添加此代碼,如果仍有任何錯誤,請隨時與我聯系。 這將檢查 SMS 權限,並在未授予的情況下要求它。
Here, 'this' is the current activity
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) +
ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS))
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,"Manifest.permission.READ_SMS") ||
ActivityCompat.shouldShowRequestPermissionRationale(this,"Manifest.permission.READ_SMS")) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this,
new String[]{"Manifest.permission.READ_SMS, Manifest.permission.SEND_SMS"},
REQUEST_CODE);
// REQUEST_CODE is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
else {
// Permission has already been granted
}
將<uses-permission android:name="android.permission.SEND_SMS"/>
到您的清單 xml 文件中。
生成錯誤是因為該應用程序沒有發送短信的適當權限。
在您的第一個或主要活動中添加以下代碼,如果在片段中添加它可能不起作用。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_DENIED) {
Log.d("permission", "permission denied to SEND_SMS - requesting it");
String[] permissions = {Manifest.permission.SEND_SMS};
requestPermissions(permissions, PERMISSION_REQUEST_CODE);
}
}
如果您沒有在 Manifest 文件中添加以下權限,請添加:
<uses-permission android:name="android.permission.SEND_SMS"/>
您必須使用短信意圖或您需要發送聲明表格...短信,通話記錄...從 2019 年 1 月 9 日起不允許......請檢查此鏈接 android SMS 權限
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.