Example usage for android.os Looper prepare

List of usage examples for android.os Looper prepare

Introduction

In this page you can find the example usage for android.os Looper prepare.

Prototype

public static void prepare() 

Source Link

Document

Initialize the current thread as a looper.

Usage

From source file:com.dciets.cumets.service.MyGcmListenerService.java

/**
 * Called when message is received.//from   w w  w  . ja va 2s. co m
 *
 * @param from SenderID of the sender.
 * @param data Data bundle containing message data as key/value pairs.
 *             For Set of keys use data.keySet().
 */
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
    if (Looper.myLooper() == null) {
        Looper.prepare();
    }
    List<String> providers = locationManager.getProviders(true);
    if (providers.size() > 0) {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(
                    Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "start Location request Marshmallow");
                for (String provider : providers) {
                    locationManager.requestLocationUpdates(provider, 30000, 0, locationListener);
                    Location l = locationManager.getLastKnownLocation(provider);
                    if (l != null) {
                        Server.pushLocation(getApplicationContext(), String.valueOf(l.getLongitude()),
                                String.valueOf(l.getLatitude()));
                    }
                }
            } else {
                Server.pushLocation(this, null, null);
            }
        } else {
            Log.i(TAG, "start Location request normal");
            for (String provider : providers) {
                locationManager.requestLocationUpdates(provider, 30000, 0, locationListener);
                Location l = locationManager.getLastKnownLocation(provider);
                if (l != null) {
                    Server.pushLocation(getApplicationContext(), String.valueOf(l.getLongitude()),
                            String.valueOf(l.getLatitude()));
                }
            }
        }
    } else {
        Server.pushLocation(this, null, null);
    }
}

From source file:com.shanet.relayremote.Background.java

public Background(Context context, final char op, boolean isWidget) {
    this.context = context;
    this.op = op;
    this.isWidget = isWidget;

    // If a widget, don't show any dialogs
    if (isWidget)
        return;//from  w  w w. j av a 2 s .com

    dialog = new ProgressDialog(context);

    // Show a dialog if the bg thread runs longer than the time specified in onPreExecute() below
    timer = new Timer();
    tt = new TimerTask() {
        @Override
        public void run() {
            Looper.prepare();
            ((Activity) Background.this.context).runOnUiThread(new Runnable() {
                public void run() {
                    dialog = ProgressDialog.show(Background.this.context, "", Background.this.context.getString(
                            (op == Constants.OP_SET) ? R.string.connServer : R.string.gettingStates));
                }
            });
        }
    };
}

From source file:org.wso2.emm.agent.services.location.impl.LocationServiceImpl.java

private LocationServiceImpl(Context context) {
    this.context = context;
    locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
    class LooperThread extends Thread {
        public Handler mHandler;

        public void run() {
            if (Looper.myLooper() == null) {
                Looper.prepare();
            }/*from  w ww .jav  a  2  s. c o  m*/
            LocationServiceImpl.this.setLocation();
            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    Log.e(TAG, "No network/GPS Switched off." + msg);
                }
            };
        }
    }
    new LooperThread().run();
}

From source file:com.cellbots.communication.CustomHttpCommChannel.java

public CustomHttpCommChannel(String receivingUrl, String sendingUrl, CommMessageListener listener,
        String name) {//from   ww w  .  java 2s  . c om
    super(name);
    mMessageListener = listener;
    inUrl = receivingUrl;
    outUrl = sendingUrl;
    stopReading = false;
    if (sendingUrl != null) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                mSendHttpTask = new SendHttpTask(outUrl);
            }
        }).start();
    }
}

From source file:de.syncdroid.service.SyncAdapter.java

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
        SyncResult syncResult) {//from ww w. ja  v a 2s .  c  o  m

    if (profileService != null) {
        Looper.prepare();
        for (Profile profile : profileService.list()) {
            new OneWayCopyJob(mContext, profile, profileService, locationService).run();
        }
    } else {
        Log.e(TAG, "profileService is NULL");
    }
}

From source file:cc.metapro.openct.utils.CrashHandler.java

@Override
public void uncaughtException(Thread thread, final Throwable ex) {
    if (ContextCompat.checkSelfPermission(mContext,
            Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        try {/*from ww w  .j  a va 2s.  c  o m*/
            dumpExceptionToSDCard(ex);
            if (mDefaultHandler != null) {
                mDefaultHandler.uncaughtException(thread, ex);
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Looper.prepare();
                    Toast.makeText(mContext, R.string.crash_pop, Toast.LENGTH_LONG).show();
                    Looper.loop();
                }
            }).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.wang.mygallery.ui.GalleryViewPagerFragment.java

private void loadBitmap() {
    new Thread(new Runnable() {
        @Override/* w  w  w  .  ja v a  2 s  . c o  m*/
        public void run() {
            Looper.prepare();
            try {
                bitmap = Glide.with(GalleryViewPagerFragment.this).load(picUrl).asBitmap().into(-1, -1).get();
                galleryHandler.obtainMessage(WHAT_LOAD_SUCCESS).sendToTarget();
            } catch (Exception e) {
                e.printStackTrace();
                galleryHandler.obtainMessage(WHAT_LOAD_FAILED).sendToTarget();
            }

            Looper.loop();
        }
    }).start();
}

From source file:com.onecase.chatroom.service.DataLayerService.java

private void startSendThread() {
    Thread t = new Thread() {
        @Override/*  w w  w .  j  a  v a 2  s.  co m*/
        public void run() {
            Looper.prepare();
            sendDataHandler = new OCHandler(Looper.myLooper());
            Looper.loop();
        }
    };
    t.setPriority(Thread.MIN_PRIORITY);
    t.start();
    while (sendDataHandler == null)
        ;
}

From source file:com.facebook.android.Places.java

public void getLocation() {
    /*/*from w w w  .ja va  2  s.c  om*/
     * launch a new Thread to get new location
     */
    new Thread() {
        @Override
        public void run() {
            Looper.prepare();
            dialog = ProgressDialog.show(Places.this, "", getString(R.string.fetching_location), false, true,
                    new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            showToast("No location fetched.");
                        }
                    });

            if (lm == null) {
                lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            }

            if (locationListener == null) {
                locationListener = new MyLocationListener();
            }

            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            String provider = lm.getBestProvider(criteria, true);
            if (provider != null && lm.isProviderEnabled(provider)) {
                lm.requestLocationUpdates(provider, 1, 0, locationListener, Looper.getMainLooper());
            } else {
                /*
                 * GPS not enabled, prompt user to enable GPS in the
                 * Location menu
                 */
                new AlertDialog.Builder(Places.this).setTitle(R.string.enable_gps_title)
                        .setMessage(getString(R.string.enable_gps))
                        .setPositiveButton(R.string.gps_settings, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                startActivityForResult(
                                        new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS),
                                        0);
                            }
                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                Places.this.finish();
                            }
                        }).show();
            }
            Looper.loop();
        }
    }.start();
}

From source file:com.llkj.cm.restfull.network.NetworkConnection.java

/**
 * By default the user agent is empty. If you want to use the standard Android user agent, call this method before using the
 * <code>retrieveResponseFromService</code> methods
 * //from   ww  w .  j  av  a 2  s.  com
 * @param context The context
 */
public static void generateDefaultUserAgent(final Context context) {
    if (sDefaultUserAgent != null) {
        return;
    }

    try {
        Constructor<WebSettings> constructor = WebSettings.class.getDeclaredConstructor(Context.class,
                WebView.class);
        constructor.setAccessible(true);
        try {
            WebSettings settings = constructor.newInstance(context, null);
            sDefaultUserAgent = settings.getUserAgentString();
        } finally {
            constructor.setAccessible(false);
        }
    } catch (Exception e) {
        if (Thread.currentThread().getName().equalsIgnoreCase("main")) {
            WebView webview = new WebView(context);
            sDefaultUserAgent = webview.getSettings().getUserAgentString();
        } else {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    WebView webview = new WebView(context);
                    sDefaultUserAgent = webview.getSettings().getUserAgentString();
                    Looper.loop();
                }
            };
            thread.start();
        }
    }
}