如何在图库中播放新媒体?

时间:2017-09-13 00:04:26

标签: android broadcastreceiver

我正在尝试通知添加到手机图库的新图片或视频。我需要获取新媒体的URI。目的是让我可以自动备份它。所以我需要一个在后台设置的注册表来持续收听或检查添加到库中的新媒体,并捕获Uri。过去常常使用广播接收器,例如:

<receiver
    android:name=".receivers.CameraEventReceiver"
    android:label="CameraEventReceiver">
    <intent-filter>

        <!-- <action android:name="com.android.camera.NEW_PICTURE" /> -->
        <action android:name="android.hardware.action.NEW_PICTURE" />

        <data android:mimeType="image/*" />
    </intent-filter>
</receiver>

然而,这在API级别24中已被弃用。我的目标是API 21-26。有人可以告诉我如何使用像Android docs这样的JobSchedulers这样做吗?

1 个答案:

答案 0 :(得分:3)

  

在上面的API级别23中,引入了作业调度来执行任务

     

所以我正在提供可以帮助你的作业调度样本

<强> JobSchedulerService.java

@SuppressLint("NewApi")
public class JobSchedulerService extends JobService {

    private Context mContext;
    private static final int ASJOBSERVICE_JOB_ID = 999;

    // A pre-built JobInfo we use for scheduling our job.
    private static JobInfo JOB_INFO = null;

    public static int a(Context context) {
        int schedule = ((JobScheduler) context.getSystemService(JobScheduler.class)).schedule(JOB_INFO);
        Log.i("PhotosContentJob", "JOB SCHEDULED!");
        return schedule;
    }

    // Schedule this job, replace any existing one.
    public static void scheduleJob(Context context) {
        if (JOB_INFO != null) {
            a(context);
        } else {
            JobScheduler js = context.getSystemService(JobScheduler.class);
            JobInfo.Builder builder = new JobInfo.Builder(ASJOBSERVICE_JOB_ID,
                    new ComponentName("YOUR_PACKAGE_NAME", JobSchedulerService.class.getName()));
            builder.addTriggerContentUri(new JobInfo.TriggerContentUri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1));
            builder.addTriggerContentUri(new JobInfo.TriggerContentUri(MediaStore.Images.Media.INTERNAL_CONTENT_URI, 1));
            builder.setTriggerContentMaxDelay(500);
            JOB_INFO = builder.build();
            js.schedule(JOB_INFO);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public boolean onStartJob(final JobParameters params) {
        mContext = this;
        // Did we trigger due to a content change?
        if (params.getTriggeredContentAuthorities() != null) {
            if (params.getTriggeredContentUris() != null) {
                // If we have details about which URIs changed, then iterate through them
                // and collect either the ids that were impacted or note that a generic
                // change has happened.
                ArrayList<String> ids = new ArrayList<>();
                for (Uri uri : params.getTriggeredContentUris()) {
                    if (uri != null) {
                        Handler handler = new Handler();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //Perform your task here
                            }
                        });
                    }
                }
                jobFinished(params, true); // see this, we are saying we just finished the job
                // We will emulate taking some time to do this work, so we can see batching happen.
                scheduleJob(this);
            }
        }
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        return false;
    }

}

在清单文件中

<service
    android:name=".services.JobSchedulerService"
    android:enabled="true"
    android:permission="android.permission.BIND_JOB_SERVICE" />

在您的MainActivity.java中,您的启动器文件

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                JobSchedulerService.scheduleJob(this);
    }
}