Start Service from Notification

前端 未结 4 843
有刺的猬
有刺的猬 2020-12-13 13:33

is it possible to start a service from a notification. The normal way of starting an activity is working perfectly, but I need some pre checks of data before actually start

4条回答
  •  無奈伤痛
    2020-12-13 14:33

    private void createNotification(String message) 
    
    {
    
       Intent intent = new Intent(this, Yourservice.class);
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
       PendingIntent pendingIntent = PendingIntent.getService(this, 0 /* Request code */, intent,
             0);
    
       NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
             .setSmallIcon(R.drawable.icond)
             .setContentTitle("Start Launcher")
             .setContentText(message)
             .setAutoCancel(true)
             .setOngoing(true)
             .setWhen(System.currentTimeMillis())
             .setContentIntent(pendingIntent);
    
       NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    
       notificationManager.notify(ID_NOTIFICATION , notificationBuilder.build());
    
    }
    

提交回复
热议问题