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.