2017-08-10 89 views
-1

我正在嘗試創建一個按鈕來發送短信到特定的號碼。然而,即使Android Studio沒有問題,按下按鈕時,應用程序也會在仿真器和物理設備上崩潰。發送短信與SmsManager不斷崩潰

錯誤沒有意義,因爲AndroidManifest.xml提供了正確的權限。

任何想法?提前致謝。

MainActivity.java

package saluta.salutapanicandreporter; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button panicButton = (Button) findViewById(R.id.panicButton); 

     panicButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       SmsManager smsManager = SmsManager.getDefault(); 
       smsManager.sendTextMessage("111-111-111", null, "RED ALERT. Reply SAFE when safe", null, null); 
      } 
     }); 
    } 
} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="saluta.salutapanicandreporter"> 

    <uses-permission android:name="android.permission.SEND_SMS" /> 
    <uses-permission android:name="android.permission.READ_SMS" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver android:name=".IncomingSms"> 
      <intent-filter> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
     </receiver> 

    </application> 

</manifest> 

錯誤

08-10 17:52:55.723 2436-2436/saluta.salutapanicandreporter E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: saluta.salutapanicandreporter, PID: 2436 
                      java.lang.SecurityException: Sending SMS message: uid 10079 does not have android.permission.SEND_SMS. 
                       at android.os.Parcel.readException(Parcel.java:1683) 
                       at android.os.Parcel.readException(Parcel.java:1636) 
                       at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:768) 
                       at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:310) 
                       at android.telephony.SmsManager.sendTextMessage(SmsManager.java:293) 
                       at saluta.salutapanicandreporter.MainActivity.sendSMS(MainActivity.java:16) 
                       at saluta.salutapanicandreporter.MainActivity.access$000(MainActivity.java:11) 
                       at saluta.salutapanicandreporter.MainActivity$1.onClick(MainActivity.java:31) 
                       at android.view.View.performClick(View.java:5610) 
                       at android.view.View$PerformClick.run(View.java:22265) 
                       at android.os.Handler.handleCallback(Handler.java:751) 
                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                       at android.os.Looper.loop(Looper.java:154) 
                       at android.app.ActivityThread.main(ActivityThread.java:6077) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
+1

請張貼錯誤。 –

+0

'java.lang.SecurityException異常:發送短信:UID 10079沒有android.permission.SEND_SMS.' –

回答

0

您是否正在測試API> = 23?有新的權限流向所謂的「在運行時」,你應該使用這個

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1); 

檢查已經

boolean permissionGranted = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED; 

more about in docs

也,我不是舒爾授予的權限,但由於KitKat(?)在設備上可能只有一個應用程序可以發送短信(仍可多次讀取)。你在操作系統設置中有一個選擇器,就像瀏覽器一樣。當你有多發短信應用程序時,他們正在檢查(應該...)他們被標記爲默認並且能夠發送短信。檢查這個線索

1

還要檢查CON例外。你在這種情況下,問題似乎是權限在你的清單文件丟失。 在你的manifest.xml添加此

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

還要檢查這個線程它可能SMS permissions

我希望它有幫助。

+0

請把它標記爲正確的,如果它解決您的問題。 –

+0

這已經在AndroidManifest.xml中。 – Myl0g

+0

檢查這種威脅它可以幫助你:https://stackoverflow.com/questions/34959285/android-send-sms-cannot-work –

0

你可能會在Android M(6)或測試您的應用程序?

如果是,那麼您需要運行時權限。 Android的工作室控制檯上顯示

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
{ 
    // For device above MarshMallow 
    boolean permission = getSMSPermission(); 
    if(permission) { 
     // If permission Already Granted 
     // Send You SMS here 
    } 
} 
else{ 
// Send Your SMS. You don't need Run time permission 
} 


public boolean getWritePermission(){ 
    boolean hasPermission = (ContextCompat.checkSelfPermission(this, 
    Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED); 
    if (!hasPermission) { 
     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 10); 
    } 
    return hasPermission; 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    switch (requestCode) 
    { 
     case 10: { 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
      { 
       // Permission is Granted 
       // Send Your SMS here 
      } 
     } 
    } 
}