2017-07-25 177 views
-1

我正在寫一個應用程序,將通過短信發送消息到輸入的號碼。但是,當我嘗試發送消息時,即使在我的清單中擁有此權限,我也會收到「用戶10074沒有android.permission.SEND_SMS」錯誤。權限發送短信android

 try { 


       SmsManager smsManager = SmsManager.getDefault(); 
       smsManager.sendTextMessage("5554", null, "hello", null, null); 
       // smsManager.sendTextMessage(number,null,matn,null,null); 

       Toast.makeText(Sms.this, "OK", Toast.LENGTH_LONG).show(); 
      } catch (Exception e) { 
       Toast.makeText(Sms.this, "Error"+e, Toast.LENGTH_LONG).show();  
      } 
     } 
    }); 

和代碼清單

<uses-permission android:name="android.permission.SEND_SMS" /> 
+1

你需要添加運行許可 – akhilesh0707

+0

檢查可以運行時間許可,如果您的目標SDK版本是'24'或以上 – Piyush

回答

0

的Android 6.0(API級別23)開始,用戶授予權限的應用程序,而應用程序正在運行,而不是當他們安裝應用程序。

試試這個要求運行時間許可

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

} 
0

請嘗試以下代碼運行許可。

呼叫checkAndroidVersion("5554");從點擊監聽

public void checkAndroidVersion(String mobile){ 
     this.mobile= mobile; 
     if (Build.VERSION.SDK_INT >= 23) { 
      int checkCallPhonePermission = ContextCompat.checkSelfPermission(RegistrationActivity.this,Manifest.permission.SEND_SMS); 
      if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){ 
       ActivityCompat.requestPermissions(RegistrationActivity.this,new String[]{Manifest.permission.SEND_SMS},SEND_SMS); 
       return; 
      }else{ 
       sendSms(mobile); 
      } 
     } else { 
      sendSms(mobile); 
     } 
    } 

private void sendSms(String mobileNo){ 
    try { 
     SmsManager smsManager = SmsManager.getDefault(); 
     smsManager.sendTextMessage(mobileNo, null, "hello", null, null); 
     // smsManager.sendTextMessage(number,null,matn,null,null); 

     Toast.makeText(Sms.this, "OK", Toast.LENGTH_LONG).show(); 
    } catch (Exception e) { 
     Toast.makeText(Sms.this, "Error"+e, Toast.LENGTH_LONG).show(); 
    } 
} 

而且覆蓋onRequestPermissionsResult方法

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    switch (requestCode) { 
     case SEND_SMS: 
      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       sendSms(mobile); 
      } else { 
       Toast.makeText(Sms.this, "SEND_SMS Denied", Toast.LENGTH_SHORT).show(); 
      } 
      break; 
     default: 
      super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    } 
}