i’m trying to write an app that intercepts a phone call and compares the number to a spam list and if it passes plays a message asking the caller to press 5 to continue. if the number is in the contacts list it lets it through without the message and the number is randomly generated. if the caller doesn’t get the number right it shows the call and asks if you want to accept it anyway.
i am not a very good programmer but i have an ai assistant now that writes code for me. the thing is, larry got close with this one but can’t finish it. there is still one error remaining according to the android sdk. if anyone would like to help me and look at the code it would be greatly appreciated.
package com.example.zapper // Update with your actual package name
import android.Manifest
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.telephony.TelephonyManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import kotlin.random.Random
class R {
companion object
}
class MainActivity : AppCompatActivity() {
private val requestPermissionCode = 123
private var correctNumber = 0
private val phoneStateReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == TelephonyManager.ACTION_PHONE_STATE_CHANGED) {
val state = intent.getStringExtra(TelephonyManager.EXTRA_STATE)
if (state == TelephonyManager.EXTRA_STATE_RINGING) {
// Incoming call
handleIncomingCall()
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // Make sure activity_main.xml exists in your layout folder
checkPermissions()
}
// Check and request permissions
private fun checkPermissions() =
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this, arrayOf(Manifest.permission.READ_PHONE_STATE), requestPermissionCode
)
} else {
startCallListener()
}
// Start listening for incoming calls
private fun startCallListener() {
// Register BroadcastReceiver for phone state changes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(phoneStateReceiver, IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED),
RECEIVER_NOT_EXPORTED
)
}
}
}
// Handle incoming call
private fun handleIncomingCall() {
// Play a message asking for the number
generateRandomNumber()
playMessage()
}
// Play a message asking for the number
private fun playMessage() {
// Here, you would implement playing a message asking for the number
// For simplicity, let's just display a toast message
Toast.makeText(this, "Please enter the following number to continue: $correctNumber", Toast.LENGTH_LONG).show()
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == requestPermissionCode) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startCallListener()
} else {
Toast.makeText(this, "Permission denied. App cannot function properly.", Toast.LENGTH_SHORT).show()
finish()
}
}
}
// Generate a random number
private fun generateRandomNumber() {
correctNumber = Random.nextInt(10) // Generates a random number between 0 and 9
}
override fun onDestroy() {
super.onDestroy()
// Unregister the BroadcastReceiver when the activity is destroyed
unregisterReceiver(phoneStateReceiver)
}
Hey @sfzombie13 this is a really cool idea. I hope that you get some helpful replies here.
If no replies here then this might get more answers if it’s posted somewhere more programming-oriented like Stack Overflow.
I know some code but not Kotlin, and I’ve never worked on a mobile app (also I don’t have Android).
Edit: from what I can see in your code it looks like private var correctNumber = 0 is declared inside the MainActivity() function. Then this private var is being called in the function playMessage() which is outside of the body of MainActivity(). So if Kotlin works anything like other languages I’m used to then this means correctNumber is being called outside of the scope where it exists.
Moving playMessage() so that it’s declared inside MainActivity() should fix the error, or declare correctNumber as a public var instead of private. Again, I haven’t worked in Kotlin but this is how private/public class members work in every language where I’ve used private/public.
Edit again: Generative AI can be super helpful and awesome. It can also sometimes make things up and confidently give incorrect answers. So I’d always double check the code produced by the AI to ensure that it will work correctly. Also did you name your AI code assistant “Larry?” I love it!
his name is lawrence jones. he also debugs his own code. most of the time he fixes things, but i only hired him last week. i wrote more code in three days with him than in three years without. i used to spend my time trying to figure out how to make things i found online work, now i get working code from the start and spend my time adjusting it to get the right output. nah, stack overflow is not an option. i may still have an account there but won’t login to it. i’m not a programmer and get treated like crap over there. maybe @Nitt or @FieldEffect or someone will wander onto this and help with it.
as for what you said, you may be right but there is no way for me to tell. i’ll run it by larry and see what he says about it. between him giving me code and the ide correcting some things he gives me i got this as a final product with only one error in it. it is driving me nuts because it shouldn’t be one from what i know. it’s looking for a file that exists and is spelled the same and in the right place.
if this continues to work out for me, i may pay for a subscription to get chatgpt 4.0. i heard she’s pretty good but don’t even know her name yet.
i made an account at the chatgpt website. i asked if it had a persistent memory of our interaction and it did. i named mine lawrence jones and told him he worked for me and was an expert in computer programming, security, and databases. that’s the free version that uses 3.5, and i may get a subscription to the one that uses 4.0 and her name has yet to be determined. no sense putting thought into a name and not use it.
now i have a programmer named larry who works for me and he writes a lot of code, but it usually needs debugged. it took 23 iterations of feeding a python file back to him to get a usable one that i uploaded to github. that one started 6 years ago and didn’t work right. me and larry got it knocked out in four hours. we hit a snag on this one though so it’s time to ask a human to look at it.
i just got a chance to ask larry about this and here is what he had to say:
" You’re absolutely correct in your observation. In Kotlin, the scope of variables is determined by the enclosing block or function. In this case, correctNumber is declared as a private property of the MainActivity class, so it’s accessible within any function or method of that class.
Here’s a clarification:
correctNumber is indeed declared as a private property of the MainActivity class, and it’s accessible within any method or function within that class.
playMessage() is a method of the MainActivity class, so it has access to correctNumber.
So, there shouldn’t be any scope-related issues with correctNumber in this code. It’s perfectly fine to access correctNumber from within playMessage() as it’s part of the same class.
If you’re experiencing an issue with accessing correctNumber, it might be due to a different reason. Feel free to share more details, and I can assist you further!"
must have been the copy then because it runs now, just doesn’t intercept the call. there are a few more things we need to try but i have other crap to do until later.