Example usage for android.os HandlerThread getLooper

List of usage examples for android.os HandlerThread getLooper

Introduction

In this page you can find the example usage for android.os HandlerThread getLooper.

Prototype

public Looper getLooper() 

Source Link

Document

This method returns the Looper associated with this thread.

Usage

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

/**
 * Service initialization/* w ww .ja va2 s.co m*/
 */
@Override
public void onCreate() {
    super.onCreate();
    Log_OC.i(TAG, "mPendingUploads size:" + mPendingUploads.size());
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    HandlerThread thread = new HandlerThread("FileUploaderThread", Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper, this);
    mBinder = new FileUploaderBinder();
}

From source file:com.example.alyshia.customsimplelauncher.MainActivity.java

public void setkioskReceiver() {
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(KioskMode.ACTION_ENABLE_KIOSK_MODE_RESULT);
    intentFilter.addAction(KioskMode.ACTION_DISABLE_KIOSK_MODE_RESULT);

    if (kioskReceiver == null) {
        kioskReceiver = new KioskReceiver(this, MainActivity.this);
        HandlerThread handlerThread = new HandlerThread("DifferentThread", Process.THREAD_PRIORITY_BACKGROUND);
        handlerThread.start();//from w w  w  .ja  v a 2  s .c o  m
        Looper looper = handlerThread.getLooper();
        kioskHandler = new Handler(looper, this);
        registerReceiver(kioskReceiver, intentFilter, null, kioskHandler);
    }

}

From source file:org.anhonesteffort.flock.MigrationService.java

@Override
public void onCreate() {
    HandlerThread thread = new HandlerThread("MigrationService", HandlerThread.NORM_PRIORITY);
    thread.start();/*from w  w w .jav  a  2s  .c  o m*/

    Looper serviceLooper = thread.getLooper();

    serviceHandler = new ServiceHandler(serviceLooper);
    notifyManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
    notificationBuilder = new NotificationCompat.Builder(getBaseContext());

    try {

        Optional<DavAccount> account = DavAccountHelper.getAccount(getBaseContext());
        Optional<MasterCipher> masterCipher = KeyHelper.getMasterCipher(getBaseContext());

        if (account.isPresent() && masterCipher.isPresent()) {
            this.account = account.get();
            this.masterCipher = masterCipher.get();
        } else
            Log.e(TAG, "ACCOUNT NOT PRESENT xxx 0.O");

    } catch (IOException e) {
        Log.e(TAG, "exception while getting MasterCipher >> " + e);
    }
}

From source file:com.mozilla.autophone.usbwatchdog.USBService.java

@Override
public void onCreate() {
    HandlerThread thread = new HandlerThread("ServiceStartArguments",
            android.os.Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();//from www  . j  a va  2  s.  c om

    int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.REBOOT);
    mPermissions = (permission == PackageManager.PERMISSION_GRANTED);

    permission = ContextCompat.checkSelfPermission(this, Manifest.permission.DUMP);
    mPermissions = mPermissions && (permission == PackageManager.PERMISSION_GRANTED);

    if (mPermissions) {
        Log.i("USBWatchdog", "Permissions granted. Using PowerManager.");
    } else {
        Log.i("USBWatchdog", "Permissions not granted. Using su.");
    }

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

From source file:com.cerema.cloud2.files.services.FileUploader.java

/**
 * Service initialization/*from ww w .ja v a2s  .  com*/
 */
@Override
public void onCreate() {
    super.onCreate();
    Log_OC.d(TAG, "Creating service");
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    HandlerThread thread = new HandlerThread("FileUploaderThread", Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper, this);
    mBinder = new FileUploaderBinder();

    // add AccountsUpdatedListener
    AccountManager am = AccountManager.get(getApplicationContext());
    am.addOnAccountsUpdatedListener(this, null, false);
}

From source file:com.bitants.wally.activities.ImageDetailsActivity.java

private void setupHandlers() {
    HandlerThread handlerThread = new HandlerThread("background");
    handlerThread.start();/*from   w ww . j a v  a  2 s.co  m*/
    backgroundHandler = new Handler(handlerThread.getLooper(), this);
    uiHandler = new Handler(getMainLooper(), this);
}

From source file:de.dcja.prettygreatmusicplayer.MusicPlaybackService.java

@Override
public synchronized void onCreate() {
    Log.i(TAG, "Music Playback Service Created!");
    isRunning = true;//from w w w.  jav a 2s .c o m
    sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

    powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PGMPWakeLock");

    random = new Random();

    mp = new MediaPlayer();
    mReadaheadThread = new ReadaheadThread();

    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            Log.i(TAG, "Song complete");
            next();
        }

    });

    onDemandMediaMetadataRetriever = new OnDemandMediaMetadataRetriever();

    // https://developer.android.com/training/managing-audio/audio-focus.html
    audioFocusListener = new PrettyGoodAudioFocusChangeListener();

    // Get permission to play audio
    am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);

    HandlerThread thread = new HandlerThread("ServiceStartArguments");
    thread.start();

    // Get the HandlerThread's Looper and use it for our Handler
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);

    // https://stackoverflow.com/questions/19474116/the-constructor-notification-is-deprecated
    // https://stackoverflow.com/questions/6406730/updating-an-ongoing-notification-quietly/15538209#15538209
    Intent resultIntent = new Intent(this, NowPlaying.class);
    resultIntent.putExtra("From_Notification", true);

    // Use the FLAG_ACTIVITY_CLEAR_TOP to prevent launching a second
    // NowPlaying if one already exists.
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);

    Builder builder = new NotificationCompat.Builder(this.getApplicationContext());

    String contentText = getResources().getString(R.string.ticker_text);
    if (songFile != null) {
        contentText = Utils.getPrettySongName(songFile);
    }

    Notification notification = builder.setContentText(contentText).setSmallIcon(R.drawable.ic_pgmp_launcher)
            .setWhen(System.currentTimeMillis()).setContentIntent(pendingIntent)
            .setContentTitle(getResources().getString(R.string.notification_title)).build();

    startForeground(uniqueid, notification);

    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            onTimerTick();
        }
    }, 0, 500L);

    Log.i(TAG, "Registering event receiver");
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    // Apparently audio registration is persistent across lots of things...
    // restarts, installs, etc.
    mAudioManager.registerMediaButtonEventReceiver(cn);
    // I tried to register this in the manifest, but it doesn't seen to
    // accept it, so I'll do it this way.
    getApplicationContext().registerReceiver(receiver, filter);

    headphoneReceiver = new HeadphoneBroadcastReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("android.intent.action.HEADSET_PLUG");
    registerReceiver(headphoneReceiver, filter);
}

From source file:com.smithdtyler.prettygoodmusicplayer.MusicPlaybackService.java

@Override
public synchronized void onCreate() {
    Log.i(TAG, "Music Playback Service Created!");
    isRunning = true;/*from  w  ww  . j a v a  2 s.c  om*/
    sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

    powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PGMPWakeLock");

    random = new Random();

    mp = new MediaPlayer();
    mReadaheadThread = new ReadaheadThread();

    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            Log.i(TAG, "Song complete");
            next();
        }

    });

    // https://developer.android.com/training/managing-audio/audio-focus.html
    audioFocusListener = new PrettyGoodAudioFocusChangeListener();

    // Get permission to play audio
    am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);

    HandlerThread thread = new HandlerThread("ServiceStartArguments");
    thread.start();

    // Get the HandlerThread's Looper and use it for our Handler
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);

    // https://stackoverflow.com/questions/19474116/the-constructor-notification-is-deprecated
    // https://stackoverflow.com/questions/6406730/updating-an-ongoing-notification-quietly/15538209#15538209
    Intent resultIntent = new Intent(this, NowPlaying.class);
    resultIntent.putExtra("From_Notification", true);
    resultIntent.putExtra(AlbumList.ALBUM_NAME, album);
    resultIntent.putExtra(ArtistList.ARTIST_NAME, artist);
    resultIntent.putExtra(ArtistList.ARTIST_ABS_PATH_NAME, artistAbsPath);

    // Use the FLAG_ACTIVITY_CLEAR_TOP to prevent launching a second
    // NowPlaying if one already exists.
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);

    Builder builder = new NotificationCompat.Builder(this.getApplicationContext());

    String contentText = getResources().getString(R.string.ticker_text);
    if (songFile != null) {
        contentText = Utils.getPrettySongName(songFile);
    }

    Notification notification = builder.setContentText(contentText).setSmallIcon(R.drawable.ic_pgmp_launcher)
            .setWhen(System.currentTimeMillis()).setContentIntent(pendingIntent)
            .setContentTitle(getResources().getString(R.string.notification_title)).build();

    startForeground(uniqueid, notification);

    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            onTimerTick();
        }
    }, 0, 500L);

    Log.i(TAG, "Registering event receiver");
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    // Apparently audio registration is persistent across lots of things...
    // restarts, installs, etc.
    mAudioManager.registerMediaButtonEventReceiver(cn);
    // I tried to register this in the manifest, but it doesn't seen to
    // accept it, so I'll do it this way.
    getApplicationContext().registerReceiver(receiver, filter);

    headphoneReceiver = new HeadphoneBroadcastReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("android.intent.action.HEADSET_PLUG");
    registerReceiver(headphoneReceiver, filter);
}

From source file:com.android.calendar.alerts.AlertService.java

@Override
public void onCreate() {
    HandlerThread thread = new HandlerThread("AlertService", Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();/* w w w.j a  v a2  s.c om*/

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

    // Flushes old fired alerts from internal storage, if needed.
    AlertUtils.flushOldAlertsFromInternalStorage(getApplication());
}

From source file:eu.fistar.sdcs.pa.PAManagerService.java

/**
 * Discover all the DA available on the system
 *///from w w w.  j a va 2 s  .c  o m
private void discoverDAs() {
    Log.i(PAAndroidConstants.PA_LOGTAG, "Starting Device Adapter discovery");

    // Create the Intent to broadcast
    Intent intent = new Intent(DA_DISCOVERY.REQUEST_ACTION);
    intent.putExtra(DA_DISCOVERY.BUNDLE_REPACT, DA_DISCOVERY.REPLY_ACTION);

    // Create the Intent Filter to receive broadcast replies
    IntentFilter filter = new IntentFilter();
    filter.addAction(DA_DISCOVERY.REPLY_ACTION);

    // Register the Broadcast Receiver for replies and set it to run in another thread
    HandlerThread ht = new HandlerThread("ht");
    ht.start();
    registerReceiver(broadcastDiscoveryDA, filter, null, new Handler(ht.getLooper()));

    // Send in broadcast the Intent for discovery
    sendBroadcast(intent);

    // Create a new Thread object to stop the discovery and also use it as a lock
    ScanningStopAndLock r = new ScanningStopAndLock();

    // Set the timeout for receiving replies
    r.start();

    // Wait for the scanning to be done before returning
    synchronized (r) {
        while (!r.isDiscoveryDone()) {
            try {
                r.wait();
            } catch (InterruptedException e) {
                // Just wait until scanning is done
            }
        }
    }

}