功能问题仅按照某些顺序工作



我是新手。我下面的应用程序有效,但由于某些原因,按钮仅按一定顺序运行。例如,当我按发送电子邮件时,它将无法正常工作。但是,如果我按SMS并取消选项,然后按电子邮件,它将起作用。对于新安装而言,这是非常有问题的。

该应用程序在功能上工作,但按钮仅按某些顺序工作。

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
    }

//在这里,此项activity是当前活动

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

代码的缩进是您问题的提示。您仅在执行OnClickListener上的CC_3之后仅在button6上设置OnClickListenerbutton3上的OnClickListener直到执行OnClickListener的CC_7方法才设置。

您需要确保将呼叫拨打到setOnClickListener()所有方法之外。这应该看起来像这样:

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

最新更新