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.syncedsynapse.kore2.jsonrpc.HostConnection.java

/**
* Calls the a method on the server//from   www .  j a va 2  s .  c  o  m
* This call is always asynchronous. The results will be posted, through the
* {@link ApiCallback callback} parameter, on the specified {@link android.os.Handler}.
*
* @param method Method object that represents the methood too call
* @param callback {@link ApiCallback} to post the response to
* @param handler {@link Handler} to invoke callbacks on
* @param <T> Method return type
*/
public <T> void execute(final ApiMethod<T> method, final ApiCallback<T> callback, final Handler handler) {
    LogUtils.LOGD(TAG, "Starting method execute. Method: " + method.getMethodName() + " on host: "
            + hostInfo.getJsonRpcHttpEndpoint());

    // Launch background thread
    Runnable command = new Runnable() {
        @Override
        public void run() {
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            if (protocol == PROTOCOL_HTTP) {
                executeThroughHTTP(method, callback, handler);
            } else {
                executeThroughTcp(method, callback, handler);
            }
        }
    };

    executorService.execute(command);
    //new Thread(command).start();
}

From source file:org.xbmc.kore.jsonrpc.HostConnection.java

/**
* Calls the a method on the server/*from w w  w  .ja  v a  2 s  .c  o m*/
* This call is always asynchronous. The results will be posted, through the
* {@link ApiCallback callback} parameter, on the specified {@link android.os.Handler}.
*
* @param method Method object that represents the methood too call
* @param callback {@link ApiCallback} to post the response to
* @param handler {@link Handler} to invoke callbacks on
* @param <T> Method return type
*/
public <T> void execute(final ApiMethod<T> method, final ApiCallback<T> callback, final Handler handler) {
    LogUtils.LOGD(TAG, "Starting method execute. Method: " + method.getMethodName() + " on host: "
            + hostInfo.getJsonRpcHttpEndpoint());

    // Launch background thread
    Runnable command = new Runnable() {
        @Override
        public void run() {
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            if (protocol == PROTOCOL_HTTP) {
                //                    executeThroughHttp(method, callback, handler);
                executeThroughOkHttp(method, callback, handler);
            } else {
                executeThroughTcp(method, callback, handler);
            }
        }
    };

    executorService.execute(command);
    //new Thread(command).start();
}

From source file:edu.stanford.mobisocial.dungbeetle.MessagingManagerThread.java

@Override
public void run() {
    ProfileScanningObjHandler profileScanningObjHandler = new ProfileScanningObjHandler();
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
    Set<Long> notSendingObjects = new HashSet<Long>();
    if (DBG)/*  ww w.ja v  a  2 s .c om*/
        Log.i(TAG, "Running...");
    mMessenger.init();
    long max_sent = -1;
    while (!interrupted()) {
        mOco.waitForChange();
        mOco.clearChanged();
        Cursor objs = mHelper.queryUnsentObjects(max_sent);
        try {
            Log.i(TAG, "Sending " + objs.getCount() + " objects...");
            if (objs.moveToFirst())
                do {
                    Long objId = objs.getLong(objs.getColumnIndexOrThrow(DbObject._ID));
                    String jsonSrc = objs.getString(objs.getColumnIndexOrThrow(DbObject.JSON));

                    max_sent = objId.longValue();
                    JSONObject json = null;
                    if (jsonSrc != null) {
                        try {
                            json = new JSONObject(jsonSrc);
                        } catch (JSONException e) {
                            Log.e(TAG, "bad json", e);
                        }
                    } else {
                        json = new JSONObject();
                    }

                    if (json != null) {
                        /*
                         * if you update latest feed here then there is a
                         * race condition between when you put a message
                         * into your db, when you actually have a connection
                         * to send the message (which is here) when other
                         * people send you messages the processing gets all
                         * out of order, so instead we update latest
                         * immediately when you add messages into your db
                         * inside DBHelper.java addToFeed();
                         */
                        // mFeedModifiedObjHandler.handleObj(mContext,
                        // feedUri, objId);

                        // TODO: Don't be fooled! This is not truly an
                        // EncodedObj
                        // and does not yet have a hash.
                        DbObj signedObj = App.instance().getMusubi().objForId(objId);
                        if (signedObj == null) {
                            Log.e(TAG, "Error, object " + objId + " not found in database");
                            notSendingObjects.add(objId);
                            continue;
                        }
                        DbEntryHandler h = DbObjects.getObjHandler(json);
                        h.afterDbInsertion(mContext, signedObj);

                        // TODO: Constraint error thrown for now b/c local
                        // user not in contacts
                        profileScanningObjHandler.handleObj(mContext, h, signedObj);
                    }

                    OutgoingMessage m = new OutgoingMsg(objs);
                    if (m.contents().getRecipients().isEmpty()) {
                        Log.w(TAG, "No addressees for direct message " + objId);
                        notSendingObjects.add(objId);
                    } else {
                        mMessenger.sendMessage(m);
                    }
                } while (objs.moveToNext());
            if (notSendingObjects.size() > 0) {
                if (DBG)
                    Log.d(TAG, "Marking " + notSendingObjects.size() + " objects sent");
                mHelper.markObjectsAsSent(notSendingObjects);
                notSendingObjects.clear();
            }
        } catch (Exception e) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
                Log.wtf(TAG, "error running notify loop", e);
            } else {
                Log.e(TAG, "error running notify loop", e);
            }
        } finally {
            objs.close();
        }
    }
    mHelper.close();
}

From source file:com.eTilbudsavis.etasdk.SyncManager.java

/**
 * Default constructor for the {@link SyncManager}
 * @param eta An Eta instance//from w w  w. j  a v a2  s .c  o  m
 */
public SyncManager(Eta eta) {
    mEta = eta;
    SyncLog.setLog(LOG);
    SyncLog.setLogSync(LOG_SYNC);
    // Create a new thread for a handler, so that i can later post content to that thread.
    mThread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND);
    mThread.start();
    mHandler = new Handler(mThread.getLooper());
    mDelivery = new HandlerDelivery(mHandler);
}

From source file:com.siahmsoft.soundroid.sdk7.services.UploadService.java

@Override
public void onCreate() {
    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    thread = new HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();//from  ww w.ja  v a 2s.  c o  m

    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);
}

From source file:com.dellingertech.andevcon.mocklocationprovider.SendMockLocationService.java

@Override
public void onCreate() {
    /*//from  w ww  . j  a va 2s.co  m
     * Load the mock location data from MockLocationConstants.java
     */
    //        mLocationArray = buildTestLocationArray(LocationUtils.WAYPOINTS_LAT,
    //                LocationUtils.WAYPOINTS_LNG, LocationUtils.WAYPOINTS_ACCURACY);

    mLocationArray = buildTestLocationArray("testdata1.csv", 1.0f);

    /*
     * Prepare to send status updates back to the main activity.
     * Get a local broadcast manager instance; broadcast intents sent via this
     * manager are only available within the this app.
     */
    mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);

    /*
     * Create a new background thread with an associated Looper that processes Message objects
     * from a MessageQueue. The Looper allows test Activities to send repeated requests to
     * inject mock locations from this Service.
     */
    mWorkThread = new HandlerThread("UpdateThread", Process.THREAD_PRIORITY_BACKGROUND);

    /*
     * Start the thread. Nothing actually runs until the Looper for this thread dispatches a
     * Message to the Handler.
     */
    mWorkThread.start();

    // Get the Looper for the thread
    mUpdateLooper = mWorkThread.getLooper();

    /*
     * Create a Handler object and pass in the Looper for the thread.
     * The Looper can now dispatch Message objects to the Handler's handleMessage() method.
     */
    mUpdateHandler = new UpdateHandler(mUpdateLooper);

    // Indicate that testing has not yet started
    mTestStarted = false;
}

From source file:edu.utexas.quietplaces.mocklocations.SendMockLocationService.java

@Override
public void onCreate() {
    /*/*from w  w w  .j  a va2  s . c  o m*/
     * Load the mock location data from MockLocationConstants.java
     */

    LocationArray testData = getTestDataFromFile();
    if (testData != null) {
        mLocationArray = buildTestLocationArray(testData.lat_list, testData.lng_list, testData.accuracy_list);
    }

    /*
     * Prepare to send status updates back to the main activity.
     * Get a local broadcast manager instance; broadcast intents sent via this
     * manager are only available within the this app.
     */
    mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);

    /*
     * Create a new background thread with an associated Looper that processes Message objects
     * from a MessageQueue. The Looper allows test Activities to send repeated requests to
     * inject mock locations from this Service.
     */
    mWorkThread = new HandlerThread("UpdateThread", Process.THREAD_PRIORITY_BACKGROUND);

    /*
     * Start the thread. Nothing actually runs until the Looper for this thread dispatches a
     * Message to the Handler.
     */
    mWorkThread.start();

    // Get the Looper for the thread
    mUpdateLooper = mWorkThread.getLooper();

    /*
     * Create a Handler object and pass in the Looper for the thread.
     * The Looper can now dispatch Message objects to the Handler's handleMessage() method.
     */
    mUpdateHandler = new UpdateHandler(mUpdateLooper);

    // Indicate that testing has not yet started
    mTestStarted = false;
}

From source file:com.inrista.loggliest.Loggly.java

private static synchronized void start() {
    if (mThread != null && mThread.isAlive())
        return;/*from   w  w w  .  j  a va2  s  . com*/

    mThread = new Thread(new Runnable() {
        @Override
        public void run() {
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            mThread.setName(THREAD_NAME);

            List<JSONObject> logBatch = new ArrayList<JSONObject>();
            while (true) {
                try {
                    JSONObject msg = mLogQueue.poll(10, TimeUnit.SECONDS);
                    if (msg != null) {
                        logBatch.add(msg);
                        while ((msg = mLogQueue.poll()) != null)
                            logBatch.add(msg);
                    }

                    long now = SystemClock.elapsedRealtime() / 1000;

                    if (!logBatch.isEmpty()) {
                        mLastLog = now;
                        mLogCounter += logBatch.size();
                        logToFile(logBatch);
                        logBatch.clear();
                    }

                    if ((now - mLastUpload) >= mUploadIntervalSecs || mLogCounter >= mUploadIntervalLogCount) {
                        postLogs();
                    }

                    if (((now - mLastLog) >= mIdleSecs) && mIdleSecs > 0 && mLastLog > 0) {
                        mThread.interrupt();
                    }

                } catch (InterruptedException e) {
                    mLogQueue.drainTo(logBatch);
                    logToFile(logBatch);
                    postLogs();
                    return;
                }
            }
        }
    });

    mThread.start();
}

From source file:com.blue.leaves.util.task.SugarTask.java

private Runnable buildRunnable(@NonNull final Integer id) {
    return new Runnable() {
        @Override//w  ww . ja  v  a  2 s. co m
        public void run() {
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

            /*
             * TODO:
             *
             * Thread safety problem.
             *
             * 
             */
            if (taskMap.containsKey(id)) {
                Message message = Message.obtain();

                try {
                    message.what = MESSAGE_FINISH;
                    message.obj = new Holder(id, taskMap.get(id).onBackground());
                } catch (Exception e) {
                    message.what = MESSAGE_BROKEN;
                    message.obj = new Holder(id, e);
                }

                post(message);
            }
        }
    };
}

From source file:edu.berkeley.eecs.cfc_tracker.test.location.SendMockLocationService.java

public void onCreate(Context ctxt) {
    /*// w ww. ja va  2s .co  m
     * Load the mock location data from MockLocationConstants.java
     */
    mLocationArray = buildTestLocationArray(LocationUtils.WAYPOINTS_LAT, LocationUtils.WAYPOINTS_LNG,
            LocationUtils.WAYPOINTS_ACCURACY);

    /*
     * Prepare to send status updates back to the main activity.
     * Get a local broadcast manager instance; broadcast intents sent via this
     * manager are only available within the this app.
     */
    mLocalBroadcastManager = LocalBroadcastManager.getInstance(ctxt);

    /*
     * Create a new background thread with an associated Looper that processes Message objects
     * from a MessageQueue. The Looper allows test Activities to send repeated requests to
     * inject mock locations from this Service.
     */
    mWorkThread = new HandlerThread("UpdateThread", Process.THREAD_PRIORITY_BACKGROUND);

    /*
     * Start the thread. Nothing actually runs until the Looper for this thread dispatches a
     * Message to the Handler.
     */
    mWorkThread.start();

    // Get the Looper for the thread
    mUpdateLooper = mWorkThread.getLooper();

    /*
     * Create a Handler object and pass in the Looper for the thread.
     * The Looper can now dispatch Message objects to the Handler's handleMessage() method.
     */
    mUpdateHandler = new UpdateHandler(mUpdateLooper);

    // Create a location client
    mGoogleApiClient = new GoogleApiClient.Builder(ctxt).addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(LocationServices.API).build();

    // Indicate that testing has not yet started
    mTestStarted = false;
}