我是新来的。下面的我的应用程序可以工作,但是由于某些原因,按钮只能按一定顺序起作用。例如,当我按发送电子邮件时,它将无法运行。但是,如果我按短信并取消该选项,然后按电子邮件,它将起作用。对于新安装,这是非常成问题的。
该应用程序可以正常工作,但是按钮只能按特定顺序工作。
package com.example.travel
import android.Manifest
import android.Manifest.permission
import android.annotation.SuppressLint
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices
import kotlinx.android.synthetic.main.content_main.*
private val MY_PERMISSIONS_LOCATION = 101
private val MY_PERMISSIONS_SMS = 105
private val MULTIPLE_PERMISSION = 110
class MainActivity : AppCompatActivity(),
ActivityCompat.OnRequestPermissionsResultCallback {
@SuppressLint("NewApi")
@RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
requestPermissions(
arrayOf(Manifest.permission.SEND_SMS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE),
MULTIPLE_PERMISSION
)
if (ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
) {
// Permission is not granted
}
if (ContextCompat.checkSelfPermission(this, permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED
) {
// Permission is not granted
}
//在这里,thisActivity是当前活动
if (ContextCompat.checkSelfPermission(
this,
permission.ACCESS_FINE_LOCATION
)
!= PackageManager.PERMISSION_GRANTED
) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(
this,
permission.ACCESS_FINE_LOCATION
)
) {
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(
this,
arrayOf(permission.ACCESS_FINE_LOCATION),
MY_PERMISSIONS_LOCATION
)
}
} else {
// Permission has already been granted
}
fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>, grantResults: IntArray
) {
when (requestCode) {
MY_PERMISSIONS_LOCATION -> {
// If request is cancelled, the result arrays are empty.
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
}
return
}
// Add other 'when' lines to check for other
// permissions this app might request.
else -> {
// Ignore all other requests.
}
}
}
button4.setOnClickListener {
val callIntent = Intent(Intent.ACTION_CALL)
callIntent.data = Uri.parse("tel:+Enter Phone Number")
startActivity(callIntent)
}
button5.setOnClickListener {
val intent2 = Intent(this, privacyActivity::class.java)
startActivity(intent2)
}
val fusedLocationClient: FusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(this)
lateinit var locationCallback: LocationCallback
fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
// Got last known location. In some rare situations this can be null.
fun onCreate(savedInstanceState: Bundle?) {
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationResult ?: return
for (location in locationResult.locations) {
}
}
}
}
var lat = location?.latitude
var lon = location?.longitude
var acc = location?.accuracy
button.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + "Enter Phone Number"))
intent.putExtra(
"sms_body",
"I have reached my destination." + " My location is: " + "Latitude: " + lat + " Longitude: " + lon
)
startActivity(intent)
}
button2.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + "Enter Phone Number"))
intent.putExtra(
"sms_body",
"I am in immediate need of assistance, " + " My location is: " + "Latitude: " + lat + " Longitude: " + lon + " Accuracy: " + acc
)
startActivity(intent)
button6.setOnClickListener {
val emailIntent = Intent(Intent.ACTION_SENDTO)
emailIntent.data = Uri.parse("mailto:Enteremailaddress")
emailIntent.putExtra (Intent.EXTRA_SUBJECT, "Safety Check-in")
emailIntent.putExtra (Intent.EXTRA_TEXT, "I have reached my destination" + " My location is: " + "Latitude: " + lat + " Longitude: " + lon);
try {
startActivity(emailIntent)
} catch (e: ActivityNotFoundException) {
Toast.makeText(this, "There are no email applications installed.", Toast.LENGTH_SHORT)
.show()
}
button3.setOnClickListener {
val emailIntenthelp = Intent(Intent.ACTION_SENDTO)
emailIntenthelp.data = Uri.parse("mailto:Enter Email")
emailIntenthelp.putExtra(Intent.EXTRA_SUBJECT, "URGENT ASSISTANCE NEEDED")
emailIntenthelp.putExtra(Intent.EXTRA_TEXT , "I am in need of immediate assistance" + " My location is: " + "Latitude: " + lat + "Longitude: " + lon)
try {
startActivity(emailIntenthelp)
} catch (e: ActivityNotFoundException) {
Toast.makeText(this, "There are no email applications installed.", Toast.LENGTH_SHORT)
.show()
}
}
}
}
}
}
}
答案 0 :(得分:0)
您的代码缩进表示您的问题。仅在执行OnClickListener
上的button6
后在OnClickListener
上设置button2
。在执行OnClickListener
的{{1}}方法之前,不会设置button3
上的OnClickListener
。
您需要确保将调用转移到button6
之外的所有方法。看起来应该像这样:
setOnClickListener()