我的 JobService 在 Android 10 中不工作,但在 Android 9 及更低版本中工作正常

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

我正在创建一个 Android 应用程序,当应用程序在后台 运行 时,当用户从系统复制到剪贴板时会发送通知。为此,我正在使用 JobService。但是我的 JobService 在 Android 10 及更高版本中不工作,但在 Android 9 及更低版本中工作正常。

这是我的 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: ");
            }
        };}

这是我的 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);
}}

你有 no background access to clipboard data on Android 10 and higher.