画廊如何获取新媒体的传播?
How to get broadcast of new media in gallery?
我正在尝试接收有关添加到 phone 图库的新图片或视频的通知。我需要获取新媒体的 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 文档所说的 JobScheduler 来做到这一点吗?
In API level 23 above Job scheduling is been introduce to perform the task
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 启动器文件中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
JobSchedulerService.scheduleJob(this);
}
}
我正在尝试接收有关添加到 phone 图库的新图片或视频的通知。我需要获取新媒体的 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 文档所说的 JobScheduler 来做到这一点吗?
In API level 23 above Job scheduling is been introduce to perform the task
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 启动器文件中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
JobSchedulerService.scheduleJob(this);
}
}