JobInfo.Builder.AddTriggerContentUri(JobInfo+TriggerContentUri) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Aggiungere un nuovo contenuto: URI che verrà monitorato con un ContentObserver e causerà l'esecuzione del processo se modificato.
[Android.Runtime.Register("addTriggerContentUri", "(Landroid/app/job/JobInfo$TriggerContentUri;)Landroid/app/job/JobInfo$Builder;", "", ApiSince=24)]
public Android.App.Job.JobInfo.Builder? AddTriggerContentUri(Android.App.Job.JobInfo.TriggerContentUri uri);
[<Android.Runtime.Register("addTriggerContentUri", "(Landroid/app/job/JobInfo$TriggerContentUri;)Landroid/app/job/JobInfo$Builder;", "", ApiSince=24)>]
member this.AddTriggerContentUri : Android.App.Job.JobInfo.TriggerContentUri -> Android.App.Job.JobInfo.Builder
Parametri
Contenuto: URI da monitorare. Questo valore non può essere Null.
Valori restituiti
- Attributi
Commenti
Aggiungere un nuovo contenuto: URI che verrà monitorato con un ContentObserver e causerà l'esecuzione del processo se modificato. Se sono presenti URI del contenuto del trigger associati a un processo, non verrà eseguito fino a quando non è stato creato un report di modifica per uno o più di essi.
Si noti che gli URI del trigger non possono essere usati in combinazione con setPeriodic(long) o setPersisted(boolean). Per monitorare continuamente le modifiche al contenuto, è necessario pianificare un nuovo Elemento JobInfo usando lo stesso ID processo e osservare gli stessi URI al posto di chiamare JobService.jobFinished(JobParameters,boolean). Tenere presente che JobScheduler.schedule(JobInfo) arresta un processo in esecuzione se usa lo stesso ID processo, quindi chiamarlo solo dopo aver completato l'elaborazione delle modifiche più recenti (in altre parole, chiamare JobScheduler.schedule(JobInfo) in cui normalmente è stato chiamato JobService.jobFinished(JobParameters,boolean). Seguendo questo modello si garantisce che non si perdano modifiche al contenuto: mentre il processo è in esecuzione, il sistema continuerà a monitorare le modifiche al contenuto e propagare le modifiche visualizzate nel processo successivo pianificato, quindi non è necessario preoccuparsi di nuove modifiche mancanti. La pianificazione del nuovo processo prima o durante l'elaborazione causerà l'arresto del processo corrente (come descritto in JobScheduler.schedule(JobInfo)), il che significa che il wakelock verrà rilasciato per il processo corrente e il processo dell'app potrebbe essere terminato perché non sarà più in un ciclo di vita di componente valido. Poiché JobScheduler.schedule(JobInfo) arresta il processo corrente, non è necessario chiamare JobService.jobFinished(JobParameters,boolean) se si chiama JobScheduler.schedule(JobInfo) usando lo stesso ID processo del processo attualmente in esecuzione.
Poiché l'impostazione di questa proprietà non è compatibile con processi periodici o persistenti, in questo modo verrà generata un'eccezione IllegalArgumentException quando si chiama build().
L'esempio seguente mostra come questa funzionalità può essere usata per monitorare le modifiche apportate alle foto in un dispositivo.
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Example stub job to monitor when there is a change to photos in the media provider.
*/
public class PhotosContentJob extends JobService {
// The root URI of the media provider, to monitor for generic changes to its content.
static final Uri MEDIA_URI = Uri.parse("content://" + MediaStore.AUTHORITY + "/");
// Path segments for image-specific URIs in the provider.
static final List<String> EXTERNAL_PATH_SEGMENTS
= MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPathSegments();
// The columns we want to retrieve about a particular image.
static final String[] PROJECTION = new String[] {
MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA
};
static final int PROJECTION_ID = 0;
static final int PROJECTION_DATA = 1;
// This is the external storage directory where cameras place pictures.
static final String DCIM_DIR = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM).getPath();
// A pre-built JobInfo we use for scheduling our job.
static final JobInfo JOB_INFO;
static {
JobInfo.Builder builder = new JobInfo.Builder(JobIds.PHOTOS_CONTENT_JOB,
new ComponentName("com.example.android.apis", PhotosContentJob.class.getName()));
// Look for specific changes to images in the provider.
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS));
// Also look for general reports of changes in the overall provider.
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(MEDIA_URI, 0));
JOB_INFO = builder.build();
}
// Fake job work. A real implementation would do some work on a separate thread.
final Handler mHandler = new Handler();
final Runnable mWorker = new Runnable() {
@Override public void run() {
scheduleJob(PhotosContentJob.this);
jobFinished(mRunningParams, false);
}
};
JobParameters mRunningParams;
// Schedule this job, replace any existing one.
public static void scheduleJob(Context context) {
JobScheduler js = context.getSystemService(JobScheduler.class);
js.schedule(JOB_INFO);
Log.i("PhotosContentJob", "JOB SCHEDULED!");
}
// Check whether this job is currently scheduled.
public static boolean isScheduled(Context context) {
JobScheduler js = context.getSystemService(JobScheduler.class);
List<JobInfo> jobs = js.getAllPendingJobs();
if (jobs == null) {
return false;
}
for (int i=0; i<jobs.size(); i++) {
if (jobs.get(i).getId() == JobIds.PHOTOS_CONTENT_JOB) {
return true;
}
}
return false;
}
// Cancel this job, if currently scheduled.
public static void cancelJob(Context context) {
JobScheduler js = context.getSystemService(JobScheduler.class);
js.cancel(JobIds.PHOTOS_CONTENT_JOB);
}
@Override
public boolean onStartJob(JobParameters params) {
Log.i("PhotosContentJob", "JOB STARTED!");
mRunningParams = params;
// Instead of real work, we are going to build a string to show to the user.
StringBuilder sb = new StringBuilder();
// Did we trigger due to a content change?
if (params.getTriggeredContentAuthorities() != null) {
boolean rescanNeeded = false;
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()) {
List<String> path = uri.getPathSegments();
if (path != null && path.size() == EXTERNAL_PATH_SEGMENTS.size()+1) {
// This is a specific file.
ids.add(path.get(path.size()-1));
} else {
// Oops, there is some general change!
rescanNeeded = true;
}
}
if (ids.size() > 0) {
// If we found some ids that changed, we want to determine what they are.
// First, we do a query with content provider to ask about all of them.
StringBuilder selection = new StringBuilder();
for (int i=0; i<ids.size(); i++) {
if (selection.length() > 0) {
selection.append(" OR ");
}
selection.append(MediaStore.Images.ImageColumns._ID);
selection.append("='");
selection.append(ids.get(i));
selection.append("'");
}
// Now we iterate through the query, looking at the filenames of
// the items to determine if they are ones we are interested in.
Cursor cursor = null;
boolean haveFiles = false;
try {
cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
PROJECTION, selection.toString(), null, null);
while (cursor.moveToNext()) {
// We only care about files in the DCIM directory.
String dir = cursor.getString(PROJECTION_DATA);
if (dir.startsWith(DCIM_DIR)) {
if (!haveFiles) {
haveFiles = true;
sb.append("New photos:\n");
}
sb.append(cursor.getInt(PROJECTION_ID));
sb.append(": ");
sb.append(dir);
sb.append("\n");
}
}
} catch (SecurityException e) {
sb.append("Error: no access to media!");
} finally {
if (cursor != null) {
cursor.close();
}
}
}
} else {
// We don't have any details about URIs (because too many changed at once),
// so just note that we need to do a full rescan.
rescanNeeded = true;
}
if (rescanNeeded) {
sb.append("Photos rescan needed!");
}
} else {
sb.append("(No photos content)");
}
Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
// We will emulate taking some time to do this work, so we can see batching happen.
mHandler.postDelayed(mWorker, 10*1000);
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
mHandler.removeCallbacks(mWorker);
return false;
}
}
Informazioni di riferimento su Android per android.app.job.JobInfo.Builder.addTriggerContentUri.
Le parti di questa pagina sono modifiche basate sul lavoro creato e condiviso dalla e usati in base ai termini descritti in Creative License 2.5 Attribution License.