2017-09-12 107 views
0

我嘗試在科特林發送短信,我寫了簡單的應用程序用下面的代碼:應用程序崩潰,當我嘗試發送短信

import android.support.v7.app.AppCompatActivity 
import android.os.Bundle 
import android.telephony.SmsManager 
import android.widget.TextView 
import kotlinx.android.synthetic.main.activity_main.* 

class MainActivity : AppCompatActivity() { 

override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    setContentView(R.layout.activity_main) 
    val textView = findViewById(R.id.mainTextView) as TextView 
    SEND.setOnClickListener { 
     textView.setText("click") 
     val sm = SmsManager.getDefault() 
     sm.sendTextMessage("123123123", null, "test", null, null) 
     textView.setText("OK") 

    } 
    } 
} 

,當我試圖通過USB電纜AndroidStudio應用不希望權限運行的應用程序儘管我已經添加了許可SEND_SMS來顯示。當我按下按鈕應用程序關閉時

+0

郵政logcat的? – Raghavendra

+0

您是否添加了運行時權限,如果您正在運行Android版本> = 23的應用程序 –

回答

0

您必須自行申請許可。 在本guide

// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity, 
      Manifest.permission.SEND_SMS) 
    != PackageManager.PERMISSION_GRANTED) { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
     Manifest.permission.SEND_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, we can request the permission. 

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

     // MY_PERMISSIONS_REQUEST_SEND_SMS is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 
-1

描述如果你不想使用運行時的權限在您的搖籃目標SDK版本設置爲21。

搖籃添加目標SDK這樣的:

android { 
    defaultConfig { 
     ..... 
     ..... 
     targetSdkVersion 21 
    } 
} 
相關問題