SHARE
TWEET

Untitled

a guest Jun 19th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Override
  2. public int onStartCommand(Intent intent, int flags, int startId) {
  3.  
  4.     Intent notifIntent = new Intent(this,MainActivity.class);
  5.     PendingIntent pendingIntent = PendingIntent.getActivity(this,
  6.             0,notifIntent,0);
  7.     notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  8.     contentView = new RemoteViews(getPackageName(), R.layout.download_notification_bar);
  9.     contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
  10.     contentView.setTextViewText(R.id.title, "Custom notification");
  11.  
  12.  
  13.     notification = new NotificationCompat.Builder(this,CHANEL_ID)
  14.             .setContentTitle("test")
  15.             .setContentText("test Againg")
  16.             .setContent(contentView)
  17.             .setSmallIcon(R.drawable.ic_launcher_foreground)
  18.             .setContentIntent(pendingIntent)
  19.             .build();
  20.  
  21.     startForeground(1,notification);
  22.  
  23.     return  START_STICKY;
  24.  
  25. }
  26.      
  27. <!-- Android Q requirement -->
  28. <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
  29.      
  30. class ForegroundServiceSample : Service() {
  31.  
  32.     companion object {
  33.         @JvmStatic
  34.         fun start(context: Context) {
  35.             ContextCompat.startForegroundService(context, Intent(context, ForegroundServiceSample::class.java))
  36.         }
  37.  
  38.         @JvmStatic
  39.         fun stop(context: Context) {
  40.             context.stopService(Intent(context, ForegroundServiceSample::class.java))
  41.         }
  42.     }
  43.  
  44.     // Foreground service notification =========
  45.  
  46.     private val foregroundNotificationId: Int = (System.currentTimeMillis() % 10000).toInt()
  47.     private val foregroundNotification by lazy {
  48.         NotificationCompat.Builder(this, foregroundNotificationChannelId)
  49.             .setSmallIcon(R.drawable.ic_sample_service)
  50.             .setPriority(NotificationCompat.PRIORITY_MIN)
  51.             .setSound(null)
  52.             .build()
  53.     }
  54.     private val foregroundNotificationChannelName by lazy {
  55.         getString(R.string.sample_service_name)
  56.     }
  57.     private val foregroundNotificationChannelDescription by lazy {
  58.         getString(R.string.sample_service_description)
  59.     }
  60.     private val foregroundNotificationChannelId by lazy {
  61.         "ForegroundServiceSample.NotificationChannel".also {
  62.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  63.                 (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).apply {
  64.                     if (getNotificationChannel(it) == null) {
  65.                         createNotificationChannel(NotificationChannel(
  66.                             it,
  67.                             foregroundNotificationChannelName,
  68.                             NotificationManager.IMPORTANCE_MIN
  69.                         ).also {
  70.                             it.description = foregroundNotificationChannelDescription
  71.                             it.lockscreenVisibility = NotificationCompat.VISIBILITY_PRIVATE
  72.                             it.vibrationPattern = null
  73.                             it.setSound(null, null)
  74.                             it.setShowBadge(false)
  75.                         })
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.     }
  81.  
  82.  
  83.     // Lifecycle ===============================
  84.  
  85.     override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  86.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  87.             startForeground(foregroundNotificationId, foregroundNotification)
  88.         }
  89.         return START_STICKY
  90.     }
  91.  
  92.     override fun onBind(intent: Intent?): IBinder? {
  93.         return null
  94.     }
  95.  
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top