如何在画廊中播放新媒体? [英] How to get broadcast of new media in gallery?

查看:74
本文介绍了如何在画廊中播放新媒体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图获得有关添加到电话库的新图片或视频的通知。我需要获取新媒体的URI。目的是让我可以自动备份它。因此,我需要在后台设置的寄存器来连续收听或检查添加到图库中的新媒体并捕获Uri。过去,这是通过广播接收器完成的,例如:

I am trying to get notified of a new picture or video added to the phone gallery. I would need to obtain the new media's URI. The purpose is so i can automatically back it up. So i need a register that is set in the background to continuously listen or check for new media added to the gallery, and captures the Uri. This used to be done with a broadcast receiver such as:

<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。有人可以告诉我如何使用Jobschedulers像android docs这样说吗?

However this was deprecated in API level 24. I target API 21-26. Can someone show me how to do this with JobSchedulers like android docs say too?

推荐答案


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

因此,我放置了可以帮助您的作业调度示例

So I am placing sample of Job Scheduling which can help you

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(BuildConfig.APPLICATION_ID, 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文件中,无论您的启动器文件是

In Your MainActivity.java which ever your launcher file

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

这篇关于如何在画廊中播放新媒体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆