androidandroid-studioandroid-jobscheduler

My JobService not working in Android 10 but Working fine in Android 9 and lower


I'm creating an Android app that sends a notification when user copy to clipboard from system while the app is running in the background. For this, I'm using JobService. But my JobService not working in Android 10 and upper but it works fine in Android 9 and lower.

Here is my ClipboardJobService java class:

public class ClipboardJobService extends JobService {
private static final String TAG = "ClipboardJobService";
private static final String CHANNEL_ID = "clipboard_copy_event";
private static final String CHANNEL_NAME = "Clipboard Copy Event";
private static final int NOTIFICATION_ID = 2342;
private NotificationManager notificationManager;
private ClipboardManager clipboardManager;

@Override
public boolean onStartJob(JobParameters params) {
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    clipboardManager.addPrimaryClipChangedListener(mOnPrimaryClipChangedListener);

    Log.e(TAG, "onStartJob: Service Started" );
    return false;
}

@Override
public boolean onStopJob(JobParameters params) {
    Log.e(TAG, "onStopJob: Service finished" );
    return true;
}

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        channel.setName("Jobless Joker");
        channel.setDescription("Go To ClipboardServiceExample App");

        notificationManager.createNotificationChannel(channel);
    }
}

private NotificationCompat.Builder getNotificationBuilder() {
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
    notificationBuilder.setContentTitle("InstaLoader");
    notificationBuilder.setContentText("This is Description");
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.drawable.ic_launcher_background);

    return notificationBuilder;
}

private ClipboardManager.OnPrimaryClipChangedListener mOnPrimaryClipChangedListener =
        new ClipboardManager.OnPrimaryClipChangedListener() {
            @Override
            public void onPrimaryClipChanged() {
                //Create notification channel
                createNotificationChannel();
                //Create Notification builder
                NotificationCompat.Builder notifyBuilder = getNotificationBuilder();
                if (clipboardManager != null)
                    notifyBuilder.setContentText(clipboardManager.getPrimaryClip().getItemAt(0).getText());
                //Notify to the notification manager
                notificationManager.notify(NOTIFICATION_ID, notifyBuilder.build());
                Log.e(TAG, "onPrimaryClipChanged: ");
            }
        };}

And here is my MainActivity class:

public class MainActivity extends AppCompatActivity {
private static final int JOB_ID = 234123;
private JobScheduler jobScheduler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);

    ComponentName serviceName = new ComponentName(getPackageName(),
            ClipboardJobService.class.getName());

    JobInfo.Builder jobBuilder = new JobInfo.Builder(JOB_ID, serviceName)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);

    JobInfo myJobInfo = jobBuilder.build();
    jobScheduler.schedule(myJobInfo);
}}

Solution

  • You have no background access to clipboard data on Android 10 and higher.