PendingIntent doesn't work in android studio - android

This is my code:
package com.example.notifsimple
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
val CHNANNEL_ID = "channel_99"
val id = 123456
#RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnNotif.setOnClickListener{
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(CHNANNEL_ID, "اسم الفناه", importance) //
val notification = NotificationCompat.Builder(this,CHNANNEL_ID)//
.setSmallIcon(R.drawable.android)
.setContentTitle("New Animal")
.setContentText("a new Cowala has been dicovered in the Amazons!")
.build()
//val intent = Intent(this,MainActivity::class.java)
// val pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
// notification.contentIntent = pendingIntent
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager.createNotificationChannel(mChannel)
mNotificationManager.notify(id, notification)
}
}
}
and the Code that i COMMENTED is the problem. when I UNcomment it the app will just give me the notification above but then exit app and tell me that "The app stopped working" and when I click on the notification doesn't do anything

Try this:
class MainActivity : AppCompatActivity() {
val CHNANNEL_ID = "channel_99"
val id = 123456
#RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnNotif.setOnClickListener{
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(CHNANNEL_ID, "اسم الفناه", importance) //
val intent = Intent(applicationContext,MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(applicationContext,0,intent,PendingIntent.FLAG_UPDATE_CURRENT) // use applicationContext, not `this`
val notification = NotificationCompat.Builder(this,CHNANNEL_ID)//
.setSmallIcon(R.drawable.android)
.setContentTitle("New Animal")
.setContentText("a new Cowala has been dicovered in the Amazons!")
.setContentIntent(pendingIntent) // this is how I set the pending intent and it works
.build()
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager.createNotificationChannel(mChannel)
mNotificationManager.notify(id, notification)
}
}
}

Related

show heads-up notification kotlin android studio

this is my code it only shows a simple notification how can i make it also show a pop up or heads-up notification.
sorry if this is so simple help if you know something
Notification.kt
`
package com.example.notifkos
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
const val notificationID = 1
const val channelID = "channel1"
const val titleExtra = "titleExtra"
const val messageExtra = "messageExtra"
class Notification : BroadcastReceiver()
{
override fun onReceive(context: Context, intent: Intent)
{
val importance = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
NotificationManager.IMPORTANCE_HIGH
} else {
TODO("VERSION.SDK_INT < N")
}
val notification = NotificationCompat.Builder(context, channelID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(intent.getStringExtra(titleExtra))
.setContentText(intent.getStringExtra(messageExtra))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.notify(notificationID, notification)
}
Mainactivity.kt
package com.example.notifkos
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.app.*
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.example.notifkos.databinding.ActivityMainBinding
import java.util.*
class MainActivity : AppCompatActivity()
{
private lateinit var binding : ActivityMainBinding
#RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel()
}
binding.submitButton.setOnClickListener { scheduleNotification() }
}
#RequiresApi(Build.VERSION_CODES.M)
private fun scheduleNotification()
{
val intent = Intent(applicationContext, Notification::class.java)
val title = binding.titleET.text.toString()
val message = binding.messageET.text.toString()
intent.putExtra(titleExtra, title)
intent.putExtra(messageExtra, message)
val pendingIntent = PendingIntent.getBroadcast(
applicationContext,
notificationID,
intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val time = getTime()
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
time,
pendingIntent
)
showAlert(time, title, message)
}
private fun showAlert(time: Long, title: String, message: String)
{
val importance = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
NotificationManager.IMPORTANCE_HIGH
} else {
TODO("VERSION.SDK_INT < N")
}
val date = Date(time)
val dateFormat = android.text.format.DateFormat.getLongDateFormat(applicationContext)
val timeFormat = android.text.format.DateFormat.getTimeFormat(applicationContext)
AlertDialog.Builder(this)
.setTitle("Notification Scheduled")
.setMessage(
"Title: " + title +
"\nMessage: " + message +
"\nAt: " + dateFormat.format(date) + " " + timeFormat.format(date))
.setPositiveButton("Okay"){_,_ ->}
.show()
}
#RequiresApi(Build.VERSION_CODES.M)
private fun getTime(): Long
{
val minute = binding.timePicker.minute
val hour = binding.timePicker.hour
val day = binding.datePicker.dayOfMonth
val month = binding.datePicker.month
val year = binding.datePicker.year
val calendar = Calendar.getInstance()
calendar.set(year, month, day, hour, minute)
return calendar.timeInMillis
}
#RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel()
{
val name = "Notif Channel"
val desc = "A Description of the Channel"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelID, name, importance)
channel.description = desc
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
`
Well, how about adding more details. Like, what are you trying to accomplish, how are you doing it, what difficulties you have found with the approach, another approaches you have tried, etc. In summary, without code isn't a good question neither is with just code. Search for the balance!
there was nothing i could get my hands on
i have read android stuio docs so please dont tell me to read that please

Android Notification for Exoplayer / like Spotify

So I am wondering I have a notification bar and it works, however, I noticed when I open Spotify and play. my app keeps playing, and also the notification does not appear in the media player notification section of Android.
How do I get my media player notification bar to play in the android media notification bar?
As you can see from the images it seems Spotify gets to go in the media player, and mine is going in the notification center.
My Player
Spotify
My Notification code:
package com.drn1.drn1_player.services
import android.app.*
import android.app.NotificationManager.IMPORTANCE_LOW
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.IBinder
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.drn1.drn1_player.DataHolder.get_Artist
import com.drn1.drn1_player.DataHolder.get_Media
import com.drn1.drn1_player.DataHolder.get_MediaPlayerImage
import com.drn1.drn1_player.DataHolder.get_Media_Type
import com.drn1.drn1_player.DataHolder.get_Song
import com.drn1.drn1_player.MainActivity
import com.drn1.drn1_player.R
import com.drn1.drn1_player.services.NotificationService
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.SimpleExoPlayer
class NotificationService : Service() {
override fun onCreate() {
super.onCreate()
player = SimpleExoPlayer.Builder(this).build()
}
override fun onTaskRemoved(rootIntent: Intent) {
super.onTaskRemoved(rootIntent)
player!!.stop()
player = null
val localBroadcastManager = LocalBroadcastManager
.getInstance(this#NotificationService)
localBroadcastManager.sendBroadcast(
Intent(
"com.durga.action.close"
)
)
stopForeground(true)
stopSelf()
}
override fun onDestroy() {
super.onDestroy()
}
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
if (intent.action == Constants.ACTION.STARTFOREGROUND_ACTION) {
} else if (intent.action == Constants.ACTION.PREV_ACTION) {
if (player != null) {
if (player!!.isPlaying) {
bigViews!!.setImageViewResource(
R.id.status_bar_play,
R.drawable.exo_controls_play
)
player!!.pause()
} else {
bigViews!!.setImageViewResource(R.id.status_bar_play, R.drawable.exo_icon_pause)
player!!.play()
}
mNotificationManager!!.notify(
Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
mBuilder!!.build()
)
}
} else if (intent.action == Constants.ACTION.PLAY_ACTION) {
// if (!get_Song().contains("With Binance.com")) {
player!!.removeMediaItem(0)
val mediaItem = MediaItem.fromUri(get_Media())
val currentTrack = get_Media()
player!!.setMediaItem(mediaItem)
player!!.prepare()
player!!.play()
showNotification()
// }
} else if (intent.action == Constants.ACTION.NEXT_ACTION) {
} else if (intent.action ==
Constants.ACTION.STOPFOREGROUND_ACTION
) {
player!!.stop()
player = null
stopForeground(true)
stopSelf()
}
return START_STICKY
}
private fun showNotification() {
bigViews = RemoteViews(
packageName,
R.layout.status_bar_expanded
)
Glide.with(this)
.asBitmap()
.load(get_MediaPlayerImage())
.into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
updateNotification(resource)
}
override fun onLoadCleared(placeholder: Drawable?) {
// this is called when imageView is cleared on lifecycle call or for
// some other reason.
// if you are referencing the bitmap somewhere else too other than this imageView
// clear it here as you can no longer have the bitmap
}
})
val notificationIntent = Intent(this#NotificationService, MainActivity::class.java)
notificationIntent.action = Constants.ACTION.MAIN_ACTION
notificationIntent.flags = (Intent.FLAG_ACTIVITY_NEW_TASK
or Intent.FLAG_ACTIVITY_CLEAR_TASK)
val pendingIntent = PendingIntent.getActivity(
this#NotificationService, 0,
notificationIntent, PendingIntent.FLAG_IMMUTABLE
)
bigViews!!.setTextViewText(R.id.status_bar_track_name, get_Song())
bigViews!!.setTextViewText(R.id.status_bar_artist_name, get_Artist())
bigViews!!.setTextViewText(R.id.status_bar_album_name, get_Media_Type())
bigViews!!.setImageViewResource(R.id.status_bar_play, R.drawable.exo_icon_pause)
val notificationClose = Intent(this#NotificationService, NotificationService::class.java)
notificationClose.action = Constants.ACTION.STOPFOREGROUND_ACTION
val notificationCloseIntent = PendingIntent.getService(
this#NotificationService, 0,
notificationClose, PendingIntent.FLAG_IMMUTABLE
)
bigViews!!.setOnClickPendingIntent(R.id.btn_close, notificationCloseIntent)
val notificationPlay = Intent(this#NotificationService, NotificationService::class.java)
notificationPlay.action = Constants.ACTION.PREV_ACTION
val notificationPlayIntent = PendingIntent.getService(
this#NotificationService, 0,
notificationPlay, PendingIntent.FLAG_IMMUTABLE
)
bigViews!!.setOnClickPendingIntent(R.id.status_bar_play, notificationPlayIntent)
mBuilder = NotificationCompat.Builder(
applicationContext, "notify_001"
)
val bigText = NotificationCompat.BigTextStyle()
mBuilder!!.setCustomContentView(bigViews)
mBuilder!!.setCustomBigContentView(bigViews)
mBuilder!!.setContentIntent(pendingIntent)
mBuilder!!.setSmallIcon(R.mipmap.drn1logo)
mBuilder!!.setContentTitle(get_Song())
mBuilder!!.setContentText(get_Artist())
mBuilder!!.priority = Notification.PRIORITY_LOW
mBuilder!!.setStyle(bigText)
mNotificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = "notify_001"
val channel = NotificationChannel(
channelId,
"Now Playing",
IMPORTANCE_LOW
)
mNotificationManager!!.createNotificationChannel(channel)
mBuilder!!.setBadgeIconType(R.drawable.drn1logo_background)
mBuilder!!.setChannelId(channelId)
mBuilder!!.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.drn1logo_background))
mBuilder!!.setSound(null)
}
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, mBuilder!!.build())
}
// use this method to update the Notification's UI
private fun updateNotification(bitmap: Bitmap) {
val api = Build.VERSION.SDK_INT
// update the icon
bigViews!!.setImageViewBitmap(R.id.status_bar_album_art, bitmap)
// update the notification
if (api < Build.VERSION_CODES.HONEYCOMB) {
mNotificationManager!!.notify(
Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
mBuilder!!.build()
)
} else if (api >= Build.VERSION_CODES.HONEYCOMB) {
mNotificationManager!!.notify(
Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
mBuilder!!.build()
)
}
}
companion object {
private const val CHANNEL_ID = "MyChannelId"
var mNotificationManager: NotificationManager? = null
var player: SimpleExoPlayer? = null
var bigViews: RemoteViews? = null
var mBuilder: NotificationCompat.Builder? = null
}
}

How to use an image URL as the image for notifications instead of images from the drawable?

I am using the following code with the notification where the images are from the drawable. But, I want to use the image URL to use the images that I have in my Firebase Storage. How can I do that. I tried adding the URL and that gives me an error.
val bitmap = BitmapFactory.decodeResource(applicationContext.resources,R.drawable.product_image)
val bitmapLargeIcon = BitmapFactory.decodeResource(applicationContext.resources,R.drawable.comp_logo)
This is the complete code.
private fun sendNotification(title: String, message: String) {
val intent: Intent = Intent(this, SplashActivity::class.java).apply {
flags=Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0,intent, 0)
val bitmap = BitmapFactory.decodeResource(applicationContext.resources,R.drawable.product_image)
val bitmapLargeIcon = BitmapFactory.decodeResource(applicationContext.resources,R.drawable.comp_logo)
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.product_image)
.setContentTitle(title)
.setContentText(message)
.setLargeIcon(bitmapLargeIcon)
.setStyle(NotificationCompat.BigPictureStyle().bigPicture(bitmap))
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
with(NotificationManagerCompat.from(this)) {
notify(notificationId, builder.build())
}
}
You can try this:
package com.tutorialsbuzz.notificationimgload
import android.app.NotificationChannel
import android.app.NotificationManager
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import java.util.concurrent.atomic.AtomicInteger
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val notifyBtn = findViewById<Button>(R.id.notifyBtn)
notifyBtn.setOnClickListener { v: View? ->
createNotification(
"Hey Hello!!",
"How are you?",
resources.getString(R.string.notification_channel)
)
}
}
private fun createNotification(
title: String, content: String,
channedId: String
) {
val notificationBuilder = NotificationCompat.Builder(applicationContext, channedId)
.setSmallIcon(R.drawable.ic_notifications_active)
.setAutoCancel(true)
.setLights(Color.BLUE, 500, 500)
.setVibrate(longArrayOf(500, 500, 500))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentTitle(title)
.setContentText(content)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
// Since android Oreo notification channel is needed.
val notificationManager = NotificationManagerCompat.from(this#MainActivity)
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
channedId,
channedId,
NotificationManager.IMPORTANCE_HIGH
)
channel.lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
notificationManager.createNotificationChannel(channel)
}
val imageUrl =
"https://i.pinimg.com/originals/1b/7e/4e/1b7e4eac8558ad54d6fe94ed4e14cb84.jpg"
Glide.with(applicationContext)
.asBitmap()
.load(imageUrl)
.into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
notificationBuilder.setLargeIcon(resource) //largeIcon
notificationBuilder.setStyle(
NotificationCompat.BigPictureStyle().bigPicture(resource)
) //Big Picture
val notification = notificationBuilder.build()
notificationManager.notify(NotificationID.iD, notification)
}
override fun onLoadCleared(placeholder: Drawable?) {}
override fun onLoadFailed(errorDrawable: Drawable?) {
super.onLoadFailed(errorDrawable)
}
})
}
internal object NotificationID {
private val c = AtomicInteger(100)
val iD: Int
get() = c.incrementAndGet()
}
}

My foreground notification service doesn't work properly if i change my package prefix name in putExtra Method why?

I used the Foreground Service code from Floating Window in localazy.com. My foreground Service didn't send correct codes to the onStartCommand() after I change the package prefix name that they in given in their code in the putExtra Method it always sends null instead of sending the EXIT and NOTE command. Actually, what is the use of the package prefix in the key name? Why did this problem happen?.
package com.krithik.floatingnote.service
import android.app.*
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.util.Log
import android.widget.Toast
import androidx.core.app.NotificationCompat
import com.krithik.floatingnote.R
const val INTENT_COMMAND = "com.localazy.quicknote.COMMAND"
const val INTENT_COMMAND_EXIT = "EXIT"
const val INTENT_COMMAND_NOTE = "NOTE"
private const val NOTIFICATION_CHANNEL_GENERAL = "note_general"
private const val CODE_FOREGROUND_SERVICE = 1
private const val CODE_EXIT_INTENT = 2
private const val CODE_NOTE_INTENT = 3
class FloatingService : Service() {
override fun onBind(intent: Intent?): IBinder? = null
/**
* Remove the foreground notification and stop the service.
*/
private fun stopService() {
stopForeground(true)
stopSelf()
}
/**
* Create and show the foreground notification.
*/
private fun showNotification() {
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val exitIntent = Intent(this, FloatingService::class.java).apply {
putExtra(INTENT_COMMAND, INTENT_COMMAND_EXIT)
}
val noteIntent = Intent(this, FloatingService::class.java).apply {
putExtra(INTENT_COMMAND, INTENT_COMMAND_NOTE)
}
val exitPendingIntent = PendingIntent.getService(
this, CODE_EXIT_INTENT, exitIntent, 0
)
val notePendingIntent = PendingIntent.getService(
this, CODE_NOTE_INTENT, noteIntent, 0
)
// From Android O, it's necessary to create a notification channel first.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
with(
NotificationChannel(
NOTIFICATION_CHANNEL_GENERAL,
getString(R.string.notification_channel_general),
NotificationManager.IMPORTANCE_DEFAULT
)
) {
lockscreenVisibility = Notification.VISIBILITY_PUBLIC
manager.createNotificationChannel(this)
}
} catch (ignored: Exception) {
// Ignore exception.
}
}
with(
NotificationCompat.Builder(
this,
NOTIFICATION_CHANNEL_GENERAL
)
) {
setContentTitle(getString(R.string.app_name))
setContentText(getString(R.string.notification_text))
setAutoCancel(false)
setOngoing(true)
setSmallIcon(R.drawable.ic_baseline_note_24)
setContentIntent(notePendingIntent)
addAction(
NotificationCompat.Action(
0,
getString(R.string.notification_exit),
exitPendingIntent
)
)
startForeground(CODE_FOREGROUND_SERVICE, build())
}
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
val command = intent.getStringExtra(INTENT_COMMAND)
Log.i("ServiceCommand", command.toString())
// Exit the service if we receive the EXIT command.
// START_NOT_STICKY is important here, we don't want
// the service to be relaunched.
if (command == INTENT_COMMAND_EXIT) {
stopService()
return START_NOT_STICKY
}
// Be sure to show the notification first for all commands.
// Don't worry, repeated calls have no effects.
showNotification()
// Show the floating window for adding a new note.
if (command == INTENT_COMMAND_NOTE) {
Toast.makeText(
this,
"Floating window to be added",
Toast.LENGTH_SHORT
).show()
}
return START_STICKY
}
}
package com.krithik.floatingnote
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.krithik.floatingnote.database.Note
import com.krithik.floatingnote.database.NoteDatabase
import com.krithik.floatingnote.database.NoteRepository
import com.krithik.floatingnote.databinding.ActivityMainBinding
import com.krithik.floatingnote.service.FloatingService
import com.krithik.floatingnote.service.INTENT_COMMAND
import com.krithik.floatingnote.viewModel.NoteViewModel
import com.krithik.floatingnote.viewModel.NoteViewModelFactory
import com.krithik.floatingnote.viewModel.RecyclerViewAdapter
class MainActivity : AppCompatActivity(), RecyclerViewAdapter.RowClickListener {
private lateinit var binding: ActivityMainBinding
private lateinit var noteViewModel: NoteViewModel
private lateinit var adapter: RecyclerViewAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startFloatingService()
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val dao = NoteDatabase.getInstance(application).noteDao
val repository = NoteRepository(dao)
val factory = NoteViewModelFactory(repository)
noteViewModel = ViewModelProvider(this, factory).get(NoteViewModel::class.java)
binding.noteViewModel = noteViewModel
binding.lifecycleOwner = this
noteViewModel.message.observe(this, Observer {
it.getContentIfNotHandled()?.let {
Toast.makeText(this, it, Toast.LENGTH_LONG).show()
}
})
initRecyclerView()
noteViewModel.noteList.observe(this, Observer {
adapter.submitList(it)
})
}
private fun initRecyclerView() {
binding.noteRecyclerView.layoutManager = LinearLayoutManager(this)
adapter = RecyclerViewAdapter(this)
binding.noteRecyclerView.adapter = adapter
}
override fun onDeleteNote(note: Note) {
noteViewModel.deleteNote(note)
}
private fun Context.startFloatingService(command: String = "") {
val intent = Intent(this, FloatingService::class.java)
if (command.isNotBlank()) intent.putExtra(INTENT_COMMAND, command)
Log.i("Command", INTENT_COMMAND + command)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.startForegroundService(intent)
} else {
this.startService(intent)
}
}
}
```

App not work in background - Ui.thread + Timer

I create easy first my App in Android Studio but i dont know why my App not work in background.
The application works very well if it is on all the time, but when I hide it, it stops working. What am I doing wrong? I try used Intent for background ready but.. i think here is my problem :<
"MojaKlasa" is empty class. If I replace "MojaKlasa" with "MainActivity" in Intent, the application starts, but when the condition from Timer is met, it stops working and turns off.
My code:
package com.example.vicky.notificationexample
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.RemoteViews
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.toast
import org.jetbrains.anko.uiThread
import java.net.URL
import java.nio.charset.Charset
import java.util.*
import java.util.concurrent.Executors
class MainActivity : AppCompatActivity() {
lateinit var notificationManager : NotificationManager
lateinit var notificationChannel : NotificationChannel
lateinit var builder : Notification.Builder
private val channelId = "com.example.vicky.notificationexample"
private val description = "Test notification"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val intent = Intent(this,MojaKlasa::class.java)
val pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
val contentView = RemoteViews(packageName,R.layout.notification_layout)
contentView.setTextViewText(R.id.tv_title,"Test")
contentView.setTextViewText(R.id.tv_content,"Notify")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = NotificationChannel(channelId,description,NotificationManager.IMPORTANCE_HIGH)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.GREEN
notificationChannel.enableVibration(false)
notificationManager.createNotificationChannel(notificationChannel)
builder = Notification.Builder(this,channelId)
.setContent(contentView)
.setSmallIcon(R.drawable.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(this.resources,R.drawable.ic_launcher))
.setContentIntent(pendingIntent)
}else{
builder = Notification.Builder(this)
.setContent(contentView)
.setSmallIcon(R.drawable.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(this.resources,R.drawable.ic_launcher))
.setContentIntent(pendingIntent)
}
val executor = Executors.newScheduledThreadPool(5)
doAsync(executorService = executor){
val result = URL("https://linkdomain/app2.php").readText()
uiThread {
toast(result)
var stare:Int = result.toInt()
Timer().scheduleAtFixedRate(object : TimerTask() {
#SuppressLint("SimpleDateFormat", "ServiceCast")
override fun run() {
val url = URL("https://linkdomain/app.php").readText(Charset.forName("ISO-8859-1"))
url.toInt();
if (url > stare.toString()) {
stare = stare.toInt()+1
val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (vibrator.hasVibrator()) { // Vibrator availability checking
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)) // New vibrate method for API Level 26 or higher
} else {
vibrator.vibrate(500) // Vibrate method for below API Level 26
}
}
notificationManager.notify(stare.toInt(),builder.build())
}
//Log.d("TAG", "url "+url)
//Log.d("TAG", "cos "+stare)
}
}, 0, 1000) //put here time 1000 milliseconds=1 second
}
}
}
fun buTestUpdateText2 (view: View) {
val destination = Intent(this#MainActivity.baseContext, MainActivity::class.java)
.also { it.putExtra("KEY", 0) }
startActivity(destination)
}
}

Categories

Resources