Example usage for android.os Process THREAD_PRIORITY_BACKGROUND

List of usage examples for android.os Process THREAD_PRIORITY_BACKGROUND

Introduction

In this page you can find the example usage for android.os Process THREAD_PRIORITY_BACKGROUND.

Prototype

int THREAD_PRIORITY_BACKGROUND

To view the source code for android.os Process THREAD_PRIORITY_BACKGROUND.

Click Source Link

Document

Standard priority background threads.

Usage

From source file:com.sdk.download.providers.downloads.DownloadThread.java

/**
 * Executes the download in a separate thread
 *//*from w  w  w  .j  av a 2 s  .co m*/
public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    State state = new State(mInfo);
    AndroidHttpClient client = null;
    PowerManager.WakeLock wakeLock = null;
    int finalStatus = Downloads.STATUS_UNKNOWN_ERROR;

    try {
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
        wakeLock.acquire();

        if (Constants.LOGV) {
            Log.v(Constants.TAG, "initiating download for " + mInfo.mUri);
        }

        client = AndroidHttpClient.newInstance(userAgent(), mContext);

        boolean finished = false;
        while (!finished) {
            Log.i(Constants.TAG, "Initiating request for download " + mInfo.mId);
            HttpGet request = new HttpGet(state.mRequestUri);
            try {
                executeDownload(state, client, request);
                finished = true;
            } catch (RetryDownload exc) {
                // fall through
            } finally {
                request.abort();
                request = null;
            }
        }

        if (Constants.LOGV) {
            Log.v(Constants.TAG, "download completed for " + mInfo.mUri);
        }
        finalizeDestinationFile(state);
        finalStatus = Downloads.STATUS_SUCCESS;
    } catch (StopRequest error) {
        // remove the cause before printing, in case it contains PII
        Log.w(Constants.TAG, "Aborting request for download " + mInfo.mId + ": " + error.getMessage());
        finalStatus = error.mFinalStatus;
        // fall through to finally block
    } catch (Throwable ex) { // sometimes the socket code throws unchecked
        // exceptions
        Log.w(Constants.TAG, "Exception for id " + mInfo.mId + ": " + ex);
        finalStatus = Downloads.STATUS_UNKNOWN_ERROR;
        // falls through to the code that reports an error
    } finally {
        if (client != null) {
            client.close();
            client = null;
        }
        cleanupDestination(state, finalStatus);
        notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter, state.mGotData,
                state.mFilename, state.mNewUri, state.mMimeType);

        DownloadHandler.getInstance().dequeueDownload(mInfo.mId);

        if (wakeLock != null) {
            wakeLock.release();
            wakeLock = null;
        }
    }
}

From source file:com.mappn.gfan.common.download.DownloadThread.java

/**
 * Executes the download in a separate thread
 *//*w  w w  .ja v  a 2  s.  c  om*/
public void run() {

    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    State state = new State(mInfo);
    AndroidHttpClient client = null;
    PowerManager.WakeLock wakeLock = null;
    int finalStatus = DownloadManager.Impl.STATUS_UNKNOWN_ERROR;

    try {
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, com.mappn.gfan.common.util.Utils.sLogTag);
        wakeLock.acquire();

        Utils.D("initiating download for " + mInfo.mUri);

        client = HttpClientFactory.get().getHttpClient();

        boolean finished = false;
        while (!finished) {

            Utils.D("Initiating request for download " + mInfo.mId + " url " + mInfo.mUri);

            HttpGet request = new HttpGet(state.mRequestUri);
            try {
                executeDownload(state, client, request);
                finished = true;
            } catch (RetryDownload exc) {
                // fall through
            } finally {
                request.abort();
                request = null;
            }
        }

        Utils.D("download completed for " + mInfo.mUri);

        if (!checkFile(state)) {
            throw new Throwable("File MD5 code is not the same as server");
        }

        finalizeDestinationFile(state);
        finalStatus = DownloadManager.Impl.STATUS_SUCCESS;
    } catch (StopRequest error) {
        // remove the cause before printing, in case it contains PII
        Utils.W("Aborting request for download " + mInfo.mId + " url: " + mInfo.mUri + " : "
                + error.getMessage());
        finalStatus = error.mFinalStatus;
        // fall through to finally block
    } catch (Throwable ex) { //sometimes the socket code throws unchecked exceptions
        Utils.W("Exception for id " + mInfo.mId + " url: " + mInfo.mUri + ": " + ex);
        // falls through to the code that reports an error
    } finally {
        if (wakeLock != null) {
            wakeLock.release();
            wakeLock = null;
        }
        if (client != null) {
            client = null;
        }
        cleanupDestination(state, finalStatus);
        notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter, state.mRedirectCount,
                state.mGotData, state.mFilename, state.mNewUri, state.mMimeType);
        mInfo.mHasActiveThread = false;
    }
}

From source file:com.digitalarx.android.files.services.FileDownloader.java

/**
 * Service initialization/*from   ww  w  .j a  va 2 s  .  com*/
 */
@Override
public void onCreate() {
    super.onCreate();
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    HandlerThread thread = new HandlerThread("FileDownloaderThread", Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper, this);
    mBinder = new FileDownloaderBinder();
}

From source file:com.wl.android.downloadwithprogressbutton.downloads.DownloadThread.java

/**
 * Executes the download in a separate thread
 *///from ww  w  . ja  v  a  2s. c om
public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    State state = new State(mInfo);
    AndroidHttpClient client = null;
    PowerManager.WakeLock wakeLock = null;
    int finalStatus = Downloads.STATUS_UNKNOWN_ERROR;

    try {
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
        wakeLock.acquire();

        if (Constants.LOGV) {
            Log.v(Constants.TAG, "initiating download for " + mInfo.mUri);
        }

        client = AndroidHttpClient.newInstance(userAgent(), mContext);

        boolean finished = false;
        while (!finished) {
            Log.i(Constants.TAG, "Initiating request for download " + mInfo.mId);
            HttpGet request = new HttpGet(state.mRequestUri);

            try {
                executeDownload(state, client, request);
                finished = true;
            } catch (RetryDownload exc) {
                // fall through
            } finally {
                request.abort();
                request = null;
            }
        }

        if (Constants.LOGV) {
            Log.v(Constants.TAG, "download completed for " + mInfo.mUri);
        }
        finalizeDestinationFile(state);
        finalStatus = Downloads.STATUS_SUCCESS;
    } catch (StopRequest error) {
        // remove the cause before printing, in case it contains PII
        Log.w(Constants.TAG, "Aborting request for download " + mInfo.mId + ": " + error.getMessage());
        finalStatus = error.mFinalStatus;
        // fall through to finally block
    } catch (Throwable ex) { // sometimes the socket code throws unchecked
        // exceptions
        Log.w(Constants.TAG, "Exception for id " + mInfo.mId + ": " + ex);
        finalStatus = Downloads.STATUS_UNKNOWN_ERROR;
        // falls through to the code that reports an error
    } finally {
        if (wakeLock != null) {
            wakeLock.release();
            wakeLock = null;
        }
        if (client != null) {
            client.close();
            client = null;
        }
        cleanupDestination(state, finalStatus);
        notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter, state.mGotData,
                state.mFilename, state.mNewUri, state.mMimeType);
        mInfo.mHasActiveThread = false;
    }
}

From source file:com.lan.nicehair.common.download.DownloadThread.java

/**
 * Executes the download in a separate thread
 *//*from w w w . jav  a2 s.co m*/
public void run() {

    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    State state = new State(mInfo);
    AndroidHttpClient client = null;
    PowerManager.WakeLock wakeLock = null;
    int finalStatus = DownloadManager.Impl.STATUS_UNKNOWN_ERROR;

    try {
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
        wakeLock.acquire();

        AppLog.d(TAG, "initiating download for " + mInfo.mUri);

        client = HttpClientFactory.get().getHttpClient();

        boolean finished = false;
        while (!finished) {

            AppLog.d(TAG, "Initiating request for download " + mInfo.mId + " url " + mInfo.mUri);

            HttpGet request = new HttpGet(state.mRequestUri);
            try {
                executeDownload(state, client, request);
                finished = true;
            } catch (RetryDownload exc) {
                // fall through
            } finally {
                request.abort();
                request = null;
            }
        }

        AppLog.d(TAG, "download completed for " + mInfo.mUri);

        if (!checkFile(state)) {
            throw new Throwable("File MD5 code is not the same as server");
        }

        finalizeDestinationFile(state);
        finalStatus = DownloadManager.Impl.STATUS_SUCCESS;
    } catch (StopRequest error) {
        // remove the cause before printing, in case it contains PII
        AppLog.e(TAG, "Aborting request for download " + mInfo.mId + " url: " + mInfo.mUri + " : "
                + error.getMessage());
        finalStatus = error.mFinalStatus;
        // fall through to finally block
    } catch (Throwable ex) { //sometimes the socket code throws unchecked exceptions
        AppLog.e(TAG, "Exception for id " + mInfo.mId + " url: " + mInfo.mUri + ": " + ex);
        // falls through to the code that reports an error
    } finally {
        if (wakeLock != null) {
            wakeLock.release();
            wakeLock = null;
        }
        if (client != null) {
            client = null;
        }
        cleanupDestination(state, finalStatus);
        notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter, state.mRedirectCount,
                state.mGotData, state.mFilename, state.mNewUri, state.mMimeType);
        mInfo.mHasActiveThread = false;
    }
}

From source file:cn.keyshare.download.core.DownloadThread.java

/**
  * Executes the download in a separate thread
  *//*www  . j a  va  2s. co m*/
 @SuppressLint("Wakelock")
 public void run() {
     Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

     State state = new State(mInfo);
     AndroidHttpClient client = null;
     PowerManager.WakeLock wakeLock = null;
     int finalStatus = Downloads.STATUS_UNKNOWN_ERROR;

     try {
         PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
         wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
         wakeLock.acquire();

         if (Constants.LOGV) {
             Log.v(Constants.TAG, "initiating download for " + mInfo.mUri);
         }

         client = AndroidHttpClient.newInstance(userAgent(), mContext);

         boolean finished = false;
         while (!finished) {
             Log.i(Constants.TAG, "Initiating request for download " + mInfo.mId);
             HttpGet request = new HttpGet(state.mRequestUri);
             try {
                 executeDownload(state, client, request);
                 finished = true;
             } catch (RetryDownload exc) {
                 // fall through
             } finally {
                 request.abort();
                 request = null;
             }
         }

         if (Constants.LOGV) {
             Log.v(Constants.TAG, "download completed for " + mInfo.mUri);
         }
         finalizeDestinationFile(state);
         finalStatus = Downloads.STATUS_SUCCESS;

     } catch (StopRequest error) {
         // remove the cause before printing, in case it contains PII
         Log.w(Constants.TAG,
                 "Aborting request for download " + mInfo.mId + ": " + "  final status is " + error.mFinalStatus
                         + " fileName is " + mInfo.mFileName + " download title is " + mInfo.mTitle
                         + error.getMessage());
         finalStatus = error.mFinalStatus;
         // fall through to finally block
     } catch (Throwable ex) { // sometimes the socket code throws unchecked
         // exceptions
         Log.w(Constants.TAG, "Exception for id " + mInfo.mId + ": " + ex);
         finalStatus = Downloads.STATUS_UNKNOWN_ERROR;
         // falls through to the code that reports an error
     } finally {
         if (wakeLock != null) {
             wakeLock.release();
             wakeLock = null;
         }
         if (client != null) {
             client.close();
             client = null;
         }
         cleanupDestination(state, finalStatus);
         notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter, state.mGotData,
                 state.mFilename, state.mNewUri, state.mMimeType);
         mInfo.mHasActiveThread = false;
     }
 }

From source file:cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.data.service.OrgSyncService.java

@Override
public void onCreate() {
    // Start up the thread running the service. Note that we create a
    // separate thread because the service normally runs in the process's
    // main thread, which we don't want to block. We also make it
    // background priority so CPU-intensive work will not disrupt our UI.
    HandlerThread thread = new HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();/* ww  w .ja v  a2  s .c om*/

    // Get the HandlerThread's Looper and use it for our Handler
    serviceLooper = thread.getLooper();
    serviceHandler = new SyncHandler(serviceLooper);
}

From source file:org.hopestarter.wallet.ui.send.SweepWalletFragment.java

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRetainInstance(true);/*ww  w  .  j  a  v a 2s .c  o m*/
    setHasOptionsMenu(true);

    backgroundThread = new HandlerThread("backgroundThread", Process.THREAD_PRIORITY_BACKGROUND);
    backgroundThread.start();
    backgroundHandler = new Handler(backgroundThread.getLooper());

    if (savedInstanceState != null) {
        restoreInstanceState(savedInstanceState);
    } else {
        final Intent intent = activity.getIntent();

        if (intent.hasExtra(SweepWalletActivity.INTENT_EXTRA_KEY)) {
            privateKeyToSweep = (VersionedChecksummedBytes) intent
                    .getSerializableExtra(SweepWalletActivity.INTENT_EXTRA_KEY);

            // delay until fragment is resumed
            handler.post(maybeDecodeKeyRunnable);
        }
    }
}

From source file:de.schildbach.wallet.ui.EncryptKeysDialogFragment.java

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    backgroundThread = new HandlerThread("backgroundThread", Process.THREAD_PRIORITY_BACKGROUND);
    backgroundThread.start();/*from www . j a  v  a  2  s .c  om*/
    backgroundHandler = new Handler(backgroundThread.getLooper());
    fingerprintHelper = new FingerprintHelper(getActivity());
}

From source file:com.simplelife.seeds.android.utils.downloadprocess.DownloadThread.java

/**
 * Executes the download in a separate thread
 *///from  w w w .  ja va2 s .  co  m
public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    State state = new State(mInfo);
    AndroidHttpClient client = null;
    PowerManager.WakeLock wakeLock = null;
    int finalStatus = Downloads.STATUS_UNKNOWN_ERROR;

    try {
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
        wakeLock.acquire();

        if (Constants.LOGV) {
            Log.v(Constants.TAG, "initiating download for " + mInfo.mUri);
        }

        client = AndroidHttpClient.newInstance(userAgent(), mContext);

        boolean finished = false;
        while (!finished) {
            Log.i(Constants.TAG, "Initiating request for download " + mInfo.mId);
            //HttpGet request = new HttpGet(state.mRequestUri);

            Log.i(Constants.TAG, "mRequestUri: " + state.mRequestUri);
            Log.i(Constants.TAG, "mRequestRef: " + state.mRequestRef);
            HttpPost request = new HttpPost(SeedsDefinitions.SEEDS_SERVER_DOWNLOADPHP);
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("ref", state.mRequestRef));
            request.setEntity(new UrlEncodedFormEntity(params, "utf-8"));

            try {
                executeDownload(state, client, request);
                finished = true;
            } catch (RetryDownload exc) {
                // fall through
            } finally {
                request.abort();
                request = null;
            }
        }

        if (Constants.LOGV) {
            Log.v(Constants.TAG, "download completed for " + mInfo.mUri);
        }
        finalizeDestinationFile(state);
        finalStatus = Downloads.STATUS_SUCCESS;
    } catch (StopRequest error) {
        // remove the cause before printing, in case it contains PII
        Log.w(Constants.TAG, "Aborting request for download " + mInfo.mId + ": " + error.getMessage());
        finalStatus = error.mFinalStatus;
        // fall through to finally block
    } catch (Throwable ex) { // sometimes the socket code throws unchecked
        // exceptions
        Log.w(Constants.TAG, "Exception for id " + mInfo.mId + ": " + ex);
        finalStatus = Downloads.STATUS_UNKNOWN_ERROR;
        // falls through to the code that reports an error
    } finally {
        if (wakeLock != null) {
            wakeLock.release();
            wakeLock = null;
        }
        if (client != null) {
            client.close();
            client = null;
        }
        cleanupDestination(state, finalStatus);
        notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter, state.mGotData,
                state.mFilename, state.mNewUri, state.mMimeType);
        mInfo.mHasActiveThread = false;
    }
}