0

I am trying to start service using PendingIntent. Notification shows but on click of it onStartCommand of service not working.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(EasyTouchService.this,"my_channel_id_01");

Intent settingsIntent = new Intent(EasyTouchService.ACTION_SETTINGS);
settingsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent settingsPendingIntent = PendingIntent.getService(EasyTouchService.this, 1, settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT); //also tried o and some others

mBuilder.setSmallIcon(R.drawable.eight_ball_img);
mBuilder.setContentTitle(getString(R.string.app_name));
mBuilder.setContentText("Open Settings");
mBuilder.setAutoCancel(false);
mBuilder.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Stop Service", stopPendingIntent);
mBuilder.addAction(android.R.drawable.ic_menu_rotate, "Show/Hide", hidePendingIntent);

mBuilder.setContentIntent(settingsPendingIntent);

mNotification = mBuilder.build();

// setUpAsForeground(message);
mNotification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;

//updateNotification("Hide Pattern");
mNotification.tickerText = getString(R.string.app_name);
int NOTIFICATION_ID = 121125;
startForeground(NOTIFICATION_ID, mNotification);

What am i doing wrong?

ColdFire
  • 6,764
  • 6
  • 35
  • 51

4 Answers4

0

set Flag as:

settingsIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Nik
  • 1,991
  • 2
  • 13
  • 30
0

Try setting your intent Flag to FLAG_ACTIVITY_SINGLE_TOP, like this:

settingsIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

For further reference/explanation see this post

Jujinko
  • 319
  • 3
  • 21
0

Try this.

Intent intent = new Intent(context, myService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

You are trying to start a Service using an implicit Intent. This doesn't work. You should see a message in your logcat that it is insecure to start a Service using an implicit Intent.

Try using an explicit Intent to start your Service. An explicit Intent specified exactly which component (class) will be launched. Something like this:

Intent settingsIntent = new Intent(EasyTouchService.ACTION_SETTINGS);
settingsIntent.setClass(EasyTouchService.this, EasyTouchService.class);
David Wasser
  • 93,459
  • 16
  • 209
  • 274