0

adblockfrag.kt

class adblockfrag : DialogFragment() {
@SuppressLint("CommitPrefEdits")
companion object {
    var ins: adblockfrag? = null
    fun getInstance(): adblockfrag? {
        return ins
    }
}

class ActionReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.getStringExtra("action")
        if (action == "action1") {
            performAction1()
        } else if (action == "action2") {
            performAction2()
        }
        //This is used to close the notification tray
        //val it = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
        //context.sendBroadcast(it)
    }

    fun performAction1() {
        println("hello")                           //<------------------Checkpoint 1
    adblockfrag.getInstance()?.notifistop()        //<------------------Checkpoint 2
    }
    fun performAction2() {println("world")}
}

override fun onStart() {
    var spotadblock = adblocktoggle
    val sharedPrefs = activity?.getSharedPreferences("save", Context.MODE_PRIVATE)
    spotadblock.isChecked = sharedPrefs!!.getBoolean("value", false)

    if (spotadblock.isChecked) {
        showNotification()
    } else {
        canclenotification()
    }

    spotadblock.setOnClickListener {
        if (spotadblock.isChecked) {
            val editor = activity?.getSharedPreferences("save", Context.MODE_PRIVATE)?.edit()
            editor!!.putBoolean("value", true)
            editor.apply()
            showNotification()
        } else {
            val editor = activity?.getSharedPreferences("save", Context.MODE_PRIVATE)?.edit()
            editor?.putBoolean("value", false)
            editor!!.apply()
            canclenotification()
        }
    }
    super.onStart()
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    super.onCreateView(inflater, container, savedInstanceState)
    return inflater.inflate(R.layout.notificationblockfrag, container, false)
}

fun notifistop() {
    val editor = activity?.getSharedPreferences("save", Context.MODE_PRIVATE)?.edit()
    editor!!.putBoolean("value", false)
    editor.commit()
    val sharedPrefs = activity?.getSharedPreferences("save", Context.MODE_PRIVATE)
    adblocktoggle.isChecked = sharedPrefs!!.getBoolean("value", false)
}

fun notifistart() {
    println("././././././././././.././/./././")
    val editor = activity?.getSharedPreferences("save", Context.MODE_PRIVATE)?.edit()
    editor!!.putBoolean("value", true)
    editor.commit()
    showNotification()
}

fun canclenotification() {
    val notificationManager =
        activity?.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.cancel(0)
}

fun showNotification() {
    val pi = PendingIntent.getActivity(activity, 0, Intent(activity, MainActivity::class.java), FLAG_UPDATE_CURRENT)

    //Stop blocking
    val intentAction = Intent(context, ActionReceiver::class.java)
    intentAction.putExtra("action","action1");
    val stopblocking:PendingIntent = getBroadcast(context,1,intentAction,PendingIntent.FLAG_UPDATE_CURRENT)

    var text = ""
    val sharedPrefs = activity?.getSharedPreferences("save", Context.MODE_PRIVATE)

    if (sharedPrefs!!.getBoolean("value", true)) {
        text = "Verse is blocking Spotify Ads"
    } else {
        text = "Verse is not blocking Spotify Ads"
    }

    val mNotificationManager =
        activity?.getSystemService(NOTIFICATION_SERVICE) as NotificationManager

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        println(Build.VERSION.SDK_INT.toString() + "  " + Build.VERSION_CODES.O.toString())
        val channel = NotificationChannel(
            "VerseId",
            "VerseChannel",
            NotificationManager.IMPORTANCE_DEFAULT
        )
        channel.description = "YOUR_NOTIFICATION_CHANNEL_DESCRIPTION"
        mNotificationManager.createNotificationChannel(channel)
    }
    val notification: Notification = NotificationCompat.Builder(activity!!, "VerseId")
        .setSmallIcon(android.R.drawable.ic_menu_report_image)
        .setContentTitle("Verse")
        .setContentText(text)
        .setOngoing(true)
        .setContentIntent(pi)
        .addAction(android.R.mipmap.sym_def_app_icon, "Stop blocking", stopblocking)
        .build()

    mNotificationManager.notify(0, notification)
}

}

I have a Toggle button named "adblocktoggle" in "notificationblockfrag.xml".
Everything works properly (Separately).
The button in the notification call the pendingintent "stopblocking".
The function call in the broadcastreceiver "ActionReceiver" does not go past checkpoint 2.No matter what.
No Error in shown in the logcat.
I am following the code by @PrinceBansal in this post.

Please let me know if the given description is not enough.

I am new to android programming.Please help get over this problem.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Santhosh
  • 81
  • 2
  • 11
  • `adblockfrag` instance is `null`. – David Wasser Jun 18 '20 at 14:18
  • Sorry? I didn't get any error message when i tried to execute the code. Please be more specific. Which line exactly cause that? – Santhosh Jun 18 '20 at 18:05
  • I'm assuming that `adblockfrag` instance is `null`. This code: `adblockfrag.getInstance()?.notifistop()` will do nothing since `getInstance()` returns `null`. Why don't you check that? Call `getInstance()` and output the value you get before calling `notifistop()`. You won't see anything in the logcat, the code just doesn't get executed. – David Wasser Jun 18 '20 at 20:42
  • So how do i call notifistop function from the broadcastreceiver? Is there any other way? – Santhosh Jun 19 '20 at 11:37
  • Did you verify that this is the problem? is `adblackfrag.getInstance()` returning `null`? – David Wasser Jun 19 '20 at 11:57
  • Have you ever set the variable `ins` in `adblockfrag` to anything? I don't see that code. Where are you initializing it? – David Wasser Jun 19 '20 at 12:00
  • In mainactivity.kt, I assigned ins = this but in the fragment class file, i tried assigning ins =activity (Because this showed error). I also tried setting ins = context.applicationcontext but it didn't help either. I tried making a new class file for the broadcastreceiver and tried calling a decoy function in the adblockfrag and it called the function and everything worked. But notifistop function never works when called from the AcrionReceiver class. If possible, is there a way to run statements in notifistop directly from the actionreceiver class? – Santhosh Jun 19 '20 at 14:47
  • You should pass a `Context` parameter into the function `notifistop()`. In `onReceive()` you get a `Context` so you can pass that. If you call the function from an `Activity` you can just pass `this`, because a `Activity extends Context`. – David Wasser Jun 19 '20 at 19:42
  • You'll need to make `notifistop()` a package level function by moving it **outside** of the class declaration. – David Wasser Jun 19 '20 at 19:48
  • This is my second project in android studio. I have no idea what you are talking about Please bear with me and post a simple example explaining the solution that you are suggesting if you have time. Thanks in advance. – Santhosh Jun 19 '20 at 20:03

0 Answers0