下面列出了android.os.AsyncTask#executeOnExecutor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@ReactMethod
public void getCount(final Callback callback) {
AsyncTask<Void,Void,Void> myAsyncTask = new AsyncTask<Void,Void,Void>() {
@Override
protected Void doInBackground(final Void ... params) {
Context context = getReactApplicationContext();
ContentResolver cr = context.getContentResolver();
ContactsProvider contactsProvider = new ContactsProvider(cr);
Integer contacts = contactsProvider.getContactsCount();
callback.invoke(contacts);
return null;
}
};
myAsyncTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
/**
* Retrieves contacts matching String.
* Uses raw URI when <code>rawUri</code> is <code>true</code>, makes assets copy otherwise.
*
* @param searchString String to match
* @param callback user provided callback to run at completion
*/
@ReactMethod
public void getContactsMatchingString(final String searchString, final Callback callback) {
AsyncTask<Void,Void,Void> myAsyncTask = new AsyncTask<Void,Void,Void>() {
@Override
protected Void doInBackground(final Void ... params) {
Context context = getReactApplicationContext();
ContentResolver cr = context.getContentResolver();
ContactsProvider contactsProvider = new ContactsProvider(cr);
WritableArray contacts = contactsProvider.getContactsMatchingString(searchString);
callback.invoke(null, contacts);
return null;
}
};
myAsyncTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
/**
* Retrieves contacts.
* Uses raw URI when <code>rawUri</code> is <code>true</code>, makes assets copy otherwise.
*
* @param callback user provided callback to run at completion
*/
private void getAllContacts(final Callback callback) {
AsyncTask<Void,Void,Void> myAsyncTask = new AsyncTask<Void,Void,Void>() {
@Override
protected Void doInBackground(final Void ... params) {
Context context = getReactApplicationContext();
ContentResolver cr = context.getContentResolver();
ContactsProvider contactsProvider = new ContactsProvider(cr);
WritableArray contacts = contactsProvider.getContacts();
callback.invoke(null, contacts);
return null;
}
};
myAsyncTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
@SuppressLint("NewApi")
private void executeTask(AsyncTask<Void, Void, Void> task) {
// If it's running with Honeycomb or greater, it must execute each
// request in a different thread
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
task.execute();
}
}
static
AsyncTask<Void, Void, IndexItem> newInstance(FeedsActivity activity, Dialog dialog, IndexItem oldIndexItem)
{
AsyncTask<Void, Void, IndexItem> task = new AsyncCheckFeed(activity, dialog, oldIndexItem);
task.executeOnExecutor(SERIAL_EXECUTOR);
return task;
}
public static <Params, Progress, Result> void executeAsyncTask(
AsyncTask<Params, Progress, Result> task, Params... params) {
if (Build.VERSION.SDK_INT >= 11) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
} else {
task.execute(params);
}
}
public static void execute(AsyncTask<Object, Integer,Object> task,Object...args) {
if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
if (threadPool==null) {
threadPool=Executors.newCachedThreadPool();
}
task.executeOnExecutor(threadPool,args);
}else {
task.execute(args);
}
}
public static void loadMusic(final Context context, final MusicLoaderListener loaderListener) {
final AsyncTask<Void, Void, Void> loadTask = new AsyncTask<Void, Void, Void>() {
String[] resp = {"", ""};
List<MediaMetaData> listMusic = new ArrayList<>();
@Override
protected Void doInBackground(Void... voids) {
//resp = getDataResponse();
String response = loadJSONFromAsset(context);
listMusic = getMusicList(response, "music");
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (loaderListener != null && listMusic != null && listMusic.size() >= 1) {
loaderListener.onLoadSuccess(listMusic);
} else {
loaderListener.onLoadFailed();
}
}
};
loadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
public static <A, B, C> void execUi(AsyncTask<A, B, C> task, A... params) {
if (task != null) {
if (VERSION.SDK_INT < 11) {
task.execute(params);
} else {
task.executeOnExecutor(uiExecutor, params);
}
}
}
public void removeAllAsync() {
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
deleteAll();
return null;
}
};
if (Build.VERSION.SDK_INT > 10) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
task.execute();
}
}
private void executeTask(AsyncTask task, DataMap... parameters) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Object[]) parameters);
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
// } else {
// task.execute();
// }
}
/**
* execute AsyncTask
*
* @param task
*/
@SuppressLint("NewApi")
public static <Params, Progress, Result> void executeAsyncTask(
AsyncTask<Params, Progress, Result> loaderTask, Params... params) {
if (loaderTask == null) {
return;
}
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
loaderTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
} else {
loaderTask.execute(params);
}
}
private void asyncLoad(String tag, ImageView imageView, AsyncTask<String, Integer, Bitmap> task) {
Set<ImageView> pendingImages = pendingImagesMap.get(tag);
if (pendingImages == null) {
pendingImages = Collections.newSetFromMap(new WeakHashMap<ImageView, Boolean>()); // create weak set
pendingImagesMap.put(tag, pendingImages);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
pendingImages.add(imageView);
imageView.setTag(tag);
imageView.setImageDrawable(null);
}
@Override
public <T> void execute(AsyncTask<T,?,?> task, T... args) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, args);
}
public static
void run(Activity activity)
{
AsyncTask<String, Void, String[][]> task = new AsyncNavigationAdapter(activity);
task.executeOnExecutor(THREAD_POOL_EXECUTOR);
}
private void execute(AsyncTask<Void, Void, ?> asyncTask) {
asyncTask.executeOnExecutor(executor);
}
private void execute(AsyncTask<Void, Void, ?> asyncTask) {
asyncTask.executeOnExecutor(executor);
}
private void execute(AsyncTask<Void, Void, ?> asyncTask) {
asyncTask.executeOnExecutor(executor);
}
@SafeVarargs
public static <T> void executeAsyncTask(AsyncTask<T, ?, ?> asyncTask, T... params)
{
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
}
private static void fetchPhotoAsync(final RecipientEntry entry, final Uri photoThumbnailUri, final BaseAdapter adapter, final ContentResolver mContentResolver) {
final AsyncTask<Void, Void, byte[]> photoLoadTask = new AsyncTask<Void, Void, byte[]>() {
@Override
protected byte[] doInBackground(Void... params) {
// First try running a query. Images for local contacts are
// loaded by sending a query to the ContactsProvider.
final Cursor photoCursor = mContentResolver.query(
photoThumbnailUri, PhotoQuery.PROJECTION, null, null, null);
if (photoCursor != null) {
try {
if (photoCursor.moveToFirst()) {
return photoCursor.getBlob(PhotoQuery.PHOTO);
}
} finally {
photoCursor.close();
}
} else {
// If the query fails, try streaming the URI directly.
// For remote directory images, this URI resolves to the
// directory provider and the images are loaded by sending
// an openFile call to the provider.
try {
InputStream is = mContentResolver.openInputStream(
photoThumbnailUri);
if (is != null) {
byte[] buffer = new byte[BUFFER_SIZE];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
int size;
while ((size = is.read(buffer)) != -1) {
baos.write(buffer, 0, size);
}
} finally {
is.close();
}
return baos.toByteArray();
}
} catch (IOException ex) {
// ignore
}
}
return null;
}
@Override
protected void onPostExecute(final byte[] photoBytes) {
entry.setPhotoBytes(photoBytes);
if (photoBytes != null) {
mPhotoCacheMap.put(photoThumbnailUri, photoBytes);
if (adapter != null)
adapter.notifyDataSetChanged();
}
}
};
photoLoadTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}