@Override public void onStart(@Nullable Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); }
@Test public void testBasic() throws Exception { ServiceHandler handler = new ServiceHandler(); Service service = new Service(); handler.registerWebService(service); Request baseRequest = org.easymock.classextension.EasyMock.createMock(Request.class); HttpServletRequest request = createMock(HttpServletRequest.class); HttpServletResponse response = createMock(HttpServletResponse.class); expect(baseRequest.isHandled()).andReturn(false); expect(request.getMethod()).andReturn("GET"); expect(request.getPathInfo()).andReturn("/"); expect(request.getParameter("bar")).andReturn("bar2"); expect(request.getParameter("baz")).andReturn(null); expect(request.getHeader("Content-Length")).andReturn("103"); expect(response.isCommitted()).andReturn(false).anyTimes(); baseRequest.setHandled(true); org.easymock.classextension.EasyMock.replay(baseRequest); replay(request, response); handler.handle(null, baseRequest, request, response); org.easymock.classextension.EasyMock.verify(baseRequest); verify(request, response); }
private boolean msgTypeUndockedPermanent(BluetoothDevice device, int startId) { // Grace period passed. Disconnect. handleUndocked(device); if (device != null) { final SharedPreferences prefs = getPrefs(); if (DEBUG) { Log.d( TAG, "DISABLE_BT_WHEN_UNDOCKED = " + prefs.getBoolean(KEY_DISABLE_BT_WHEN_UNDOCKED, false)); } if (prefs.getBoolean(KEY_DISABLE_BT_WHEN_UNDOCKED, false)) { if (hasOtherConnectedDevices(device)) { // Don't disable BT if something is connected prefs.edit().remove(KEY_DISABLE_BT_WHEN_UNDOCKED).apply(); } else { // BT was disabled when we first docked if (DEBUG) { Log.d(TAG, "QUEUED BT DISABLE"); } // Queue a delayed msg to disable BT Message newMsg = mServiceHandler.obtainMessage(MSG_TYPE_DISABLE_BT, 0, startId, null); mServiceHandler.sendMessageDelayed(newMsg, DISABLE_BT_GRACE_PERIOD); return true; } } } return false; }
@Override protected Void doInBackground(Void... arg0) { ServiceHandler sh = new ServiceHandler(); String jsonStr = sh.makeServiceCall(checkoutUrl, ServiceHandler.GET); Log.d("Response", "<" + jsonStr); if (jsonStr != null) { try { JSONObject jsonObj = new JSONObject(jsonStr); String err = jsonObj.getString("error"); String msg = jsonObj.getString("msg"); Log.d("err", err); if (err.equals("false")) { flag = 1; } } catch (JSONException e) { e.printStackTrace(); } } else { Log.e("ServiceHandler", "Could not get data"); } return null; }
/** * Entry point to add a new operation to the queue of operations. * * <p>New operations are added calling to startService(), resulting in a call to this method. This * ensures the service will keep on working although the caller activity goes away. */ @Override public int onStartCommand(Intent intent, int flags, int startId) { Log_OC.d(TAG, "Starting command with id " + startId); // WIP: for the moment, only SYNC_FOLDER is expected here; // the rest of the operations are requested through the Binder if (ACTION_SYNC_FOLDER.equals(intent.getAction())) { if (!intent.hasExtra(EXTRA_ACCOUNT) || !intent.hasExtra(EXTRA_REMOTE_PATH)) { Log_OC.e(TAG, "Not enough information provided in intent"); return START_NOT_STICKY; } Account account = intent.getParcelableExtra(EXTRA_ACCOUNT); String remotePath = intent.getStringExtra(EXTRA_REMOTE_PATH); Pair<Account, String> itemSyncKey = new Pair<Account, String>(account, remotePath); Pair<Target, RemoteOperation> itemToQueue = newOperation(intent); if (itemToQueue != null) { mSyncFolderHandler.add( account, remotePath, (SynchronizeFolderOperation) itemToQueue.second); Message msg = mSyncFolderHandler.obtainMessage(); msg.arg1 = startId; msg.obj = itemSyncKey; mSyncFolderHandler.sendMessage(msg); } } else { Message msg = mOperationsHandler.obtainMessage(); msg.arg1 = startId; mOperationsHandler.sendMessage(msg); } return START_NOT_STICKY; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("ServiceStartArguments", "Starting #" + startId + ": " + intent.getExtras()); Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.arg2 = flags; msg.obj = intent.getExtras(); mServiceHandler.sendMessage(msg); Log.i("ServiceStartArguments", "Sending: " + msg); // For the start fail button, we will simulate the process dying // for some reason in onStartCommand(). if (intent.getBooleanExtra("fail", false)) { // Don't do this if we are in a retry... the system will // eventually give up if we keep crashing. if ((flags & START_FLAG_RETRY) == 0) { // Since the process hasn't finished handling the command, // it will be restarted with the command again, regardless of // whether we return START_REDELIVER_INTENT. Process.killProcess(Process.myPid()); } } // Normally we would consistently return one kind of result... // however, here we will select between these two, so you can see // how they impact the behavior. Try killing the process while it // is in the middle of executing the different commands. return intent.getBooleanExtra("redeliver", false) ? START_REDELIVER_INTENT : START_NOT_STICKY; }
private boolean msgTypeDocked(BluetoothDevice device, final int state, final int startId) { if (DEBUG) { // TODO figure out why hasMsg always returns false if device // is supplied Log.d( TAG, "1 Has undock perm msg = " + mServiceHandler.hasMessages(MSG_TYPE_UNDOCKED_PERMANENT, mDevice)); Log.d( TAG, "2 Has undock perm msg = " + mServiceHandler.hasMessages(MSG_TYPE_UNDOCKED_PERMANENT, device)); } mServiceHandler.removeMessages(MSG_TYPE_UNDOCKED_PERMANENT); mServiceHandler.removeMessages(MSG_TYPE_DISABLE_BT); getPrefs().edit().remove(KEY_DISABLE_BT).apply(); if (device != null) { if (!device.equals(mDevice)) { if (mDevice != null) { // Not expected. Cleanup/undock existing handleUndocked(mDevice); } mDevice = device; // Register first in case LocalBluetoothProfileManager // becomes ready after isManagerReady is called and it // would be too late to register a service listener. mProfileManager.addServiceListener(this); if (mProfileManager.isManagerReady()) { handleDocked(device, state, startId); // Not needed after all mProfileManager.removeServiceListener(this); } else { final BluetoothDevice d = device; mRunnable = new Runnable() { public void run() { handleDocked(d, state, startId); // FIXME: WTF runnable here? } }; return true; } } } else { // display dialog to enable dock for media audio only in the case of low end docks and // if not already selected by user int dockAudioMediaEnabled = Settings.Global.getInt( getContentResolver(), Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, -1); if (dockAudioMediaEnabled == -1 && state == Intent.EXTRA_DOCK_STATE_LE_DESK) { handleDocked(null, state, startId); return true; } } return false; }
/** Send message. */ @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(LOG_TAG, "onStartCommand of DarshakService is called."); Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; mServiceHandler.sendMessage(msg); return START_STICKY; }
@Override public void onStart(Intent intent, int startId) { mResultCode = intent != null ? intent.getIntExtra("result", 0) : 0; Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); }
@Override public int onStartCommand(Intent intent, int flags, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.what = MSG_UPDATE_AUTO; mServiceHandler.sendMessage(msg); return START_STICKY; }
@Override protected Void doInBackground(Void... arg0) { // Creating service handler class that we made for http requests ServiceHandler sh = new ServiceHandler(); String jsonStr = sh.makeServiceCall(url_flight, ServiceHandler.GET); Log.d("Response: ", "> " + jsonStr); beerList = beerTypes.randomFlight(jsonStr); return null; }
@Override public void onStart(Intent intent, int startId) { if (Log.DEBUG) Log.v("SMSReceiverService: onStart()"); mResultCode = intent != null ? intent.getIntExtra("result", 0) : 0; Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); }
/** Hook method called each time a Started Service is sent an Intent via startService(). */ public int onStartCommand(Intent intent, int flags, int startId) { // Create a Message that will be sent to ServiceHandler to // retrieve animagebased on the URI in the Intent. Message message = mServiceHandler.makeDownloadMessage(intent, startId); // Send the Message to ServiceHandler to retrieve an image // based on contents of the Intent. mServiceHandler.sendMessage(message); // Don't restart the DownloadService automatically if its // process is killed while it's running. return Service.START_NOT_STICKY; }
@Override protected Void doInBackground(Void... arg0) { // Creating service handler class instance ServiceHandler mServiceHandler = new ServiceHandler(); // Making a request to url and getting response String jsonStr = mServiceHandler.makeServiceCall(url, ServiceHandler.GET); Log.d("Response: ", "> " + jsonStr); if (jsonStr != null) { try { names = new ArrayList<String>(); JSONObject jsonObj = new JSONObject(jsonStr); // Getting JSON Array node allBooksJSON = jsonObj.getJSONArray(TAG_BOOKS); // looping through All Contacts for (int i = 0; i < allBooksJSON.length(); i++) { JSONObject c = allBooksJSON.getJSONObject(i); String name = c.getString(TAG_NAME); names.add(name); String aut = c.getString(TAG_AUT); String year = c.getString(TAG_YEAR); String imgname = c.getString(TAG_IMG); // tmp hashmap for single contact HashMap<String, String> oneRecord = new HashMap<String, String>(); // adding each child node to HashMap key => value oneRecord.put(TAG_NAME, name); oneRecord.put(TAG_AUT, aut); oneRecord.put(TAG_YEAR, year); oneRecord.put(TAG_IMG, imgname); // adding contact to contact list bookList.add(oneRecord); } } catch (JSONException e) { e.printStackTrace(); } } else { Log.e("ServiceHandler", "Couldn't get any data from the url"); } return null; }
@Override public void onStart(Intent intent, int startId) { Log.v("TTSNotifierService", "onStart()"); if (myTts == null) { try { myTts = new TextToSpeech(context, ttsInitListener); } catch (java.lang.ExceptionInInitializerError e) { e.printStackTrace(); } } if (mPrefs.getBoolean("cbxChangeLanguage", false)) setLanguageTts(myLanguage.getLocale()); Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); }
@Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this, "Texting service started", Toast.LENGTH_SHORT).show(); mServiceHandler.sendEmptyMessage(1); return START_STICKY; }
@Override public void onDestroy() { Log_OC.v(TAG, "Destroying service"); // Saving cookies try { OwnCloudClientManagerFactory.getDefaultSingleton() .saveAllClients(this, MainApp.getAccountType()); // TODO - get rid of these exceptions } catch (AccountNotFoundException e) { e.printStackTrace(); } catch (AuthenticatorException e) { e.printStackTrace(); } catch (OperationCanceledException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mUndispatchedFinishedOperations.clear(); mOperationsBinder = null; mOperationsHandler.getLooper().quit(); mOperationsHandler = null; mSyncFolderHandler.getLooper().quit(); mSyncFolderHandler = null; super.onDestroy(); }
protected void execute(Context ctx, HashMap<String, Object> map) throws Exception { { Object plugin = ctx.getPlugins(pluginName); Class<?>[] parameterTypes = methodArguments.getType(); Method method = plugin.getClass().getMethod(methodName, parameterTypes); for (int i = 0; i < parameterTypes.length; i++) { if (parameterTypes[i] == Context.class && methodArguments.getArgument()[i].equals("Context")) methodArguments.getArgument()[i] = ctx; else if (parameterTypes[i] == HashMap.class && methodArguments.getArgument()[i].equals("Connector Map")) methodArguments.getArgument()[i] = map; } Object rtn = null; try { rtn = method.invoke(plugin, methodArguments.getArgument()); } catch (Exception e) { throw new Exception("Component execute exception."); } if (isSetReturnValue) map.put(returnMapKey, rtn); } if (ctx.isTest()) { ctx.offerTestMessage(getHandlerID(), "Plugin : " + map + "", false, false); } super.execute(ctx, map); }
@Override public int onStartCommand(Intent intent, int flags, int startId) { final Message msg = new Message(); msg.arg1 = startId; msg.obj = intent; mHandler.sendMessage(msg); return START_STICKY; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { // Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); // For each start request, send a message to start a job and deliver the // start ID so we know which request we're stopping when we finish the // job Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; mServiceHandler.sendMessage(msg); Log.d(DEBUG_TAG, ">>>onStartCommand()"); sharedPreferences = getSharedPreferences("LOGIN_DETAIL", 0); CLIENT_ID = sharedPreferences.getString("CLIENTID", ""); UpdateCarCurrentLocation(CLIENT_ID); // If we get killed, after returning from here, restart return START_STICKY; }
/** * Entry point to add a new operation to the queue of operations. * * <p>New operations are added calling to startService(), resulting in a call to this method. This * ensures the service will keep on working although the caller activity goes away. * * <p>IMPORTANT: the only operations performed here right now is {@link GetSharedFilesOperation}. * The class is taking advantage of it due to time constraints. */ @Override public int onStartCommand(Intent intent, int flags, int startId) { if (!intent.hasExtra(EXTRA_ACCOUNT) && !intent.hasExtra(EXTRA_SERVER_URL)) { Log_OC.e(TAG, "Not enough information provided in intent"); return START_NOT_STICKY; } try { Account account = intent.getParcelableExtra(EXTRA_ACCOUNT); String serverUrl = intent.getStringExtra(EXTRA_SERVER_URL); Target target = new Target(account, (serverUrl == null) ? null : Uri.parse(serverUrl)); RemoteOperation operation = null; String action = intent.getAction(); if (action.equals(ACTION_CREATE_SHARE)) { // Create Share String remotePath = intent.getStringExtra(EXTRA_REMOTE_PATH); Intent sendIntent = intent.getParcelableExtra(EXTRA_SEND_INTENT); if (remotePath.length() > 0) { operation = new CreateShareOperation( remotePath, ShareType.PUBLIC_LINK, "", false, "", 1, sendIntent); } } else if (action.equals(ACTION_UNSHARE)) { // Unshare file String remotePath = intent.getStringExtra(EXTRA_REMOTE_PATH); if (remotePath.length() > 0) { operation = new UnshareLinkOperation(remotePath, this.getApplicationContext()); } } else { // nothing we are going to handle return START_NOT_STICKY; } mPendingOperations.add(new Pair<Target, RemoteOperation>(target, operation)); // sendBroadcastNewOperation(target, operation); Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; mServiceHandler.sendMessage(msg); } catch (IllegalArgumentException e) { Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage()); return START_NOT_STICKY; } return START_NOT_STICKY; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { // Intent redelivery PowerManager.WakeLock wl = getLock(this); if (!wl.isHeld()) wl.acquire(); // Get command final Command cmd = (intent == null ? Command.start : (Command) intent.getSerializableExtra(EXTRA_COMMAND)); Log.i(TAG, "Start intent=" + intent + " command=" + cmd + " vpn=" + (vpn != null)); // Queue command Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); return (cmd == Command.stop ? START_NOT_STICKY : START_STICKY); }
@Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(tag, "onStartCommand"); // Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); // For each start request, send a message to start a job and deliver the // start ID so we know which request we're stopping when we finish the job InsiderrService.indexSleepTimes = 0; InsiderrService.serviceIntent = intent; if (InsiderrService.mStarting) { Log.d(tag, "onStartCommand -- message"); Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; mServiceHandler.sendMessage(msg); } InsiderrService.mStarting = false; // If we get killed, after returning from here, restart return START_STICKY; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { while (!mInitialized) { try { Thread.sleep(100); } catch (InterruptedException e) { } } if (intent == null) { return Service.START_NOT_STICKY; } Message msg = mHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent.getExtras(); mHandler.sendMessage(msg); // Try again later if we are killed before we can finish scanning. return Service.START_REDELIVER_INTENT; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { // Keep awake getLock(this).acquire(); // Handle service restart if (intent == null) { Log.i(TAG, "Restart"); // Get enabled SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); boolean enabled = prefs.getBoolean("enabled", false); // Recreate intent intent = new Intent(this, SinkholeService.class); intent.putExtra(EXTRA_COMMAND, enabled ? Command.start : Command.stop); } Command cmd = (Command) intent.getSerializableExtra(EXTRA_COMMAND); String reason = intent.getStringExtra(EXTRA_REASON); Log.i( TAG, "Start intent=" + intent + " command=" + cmd + " reason=" + reason + " vpn=" + (vpn != null)); // Queue command Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; msg.what = MSG_SERVICE_INTENT; mServiceHandler.sendMessage(msg); return START_STICKY; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { // When receiving a new intent, delay the schedule until 5 seconds from now. mServiceHandler.removeMessages(0); mServiceHandler.sendEmptyMessageDelayed(0, SCHEDULE_UPDATE_DELAY_MILLIS); // Remove pending updates involving this session ID. String sessionId = intent.getStringExtra(EXTRA_SESSION_ID); Iterator<Intent> updatesIterator = mScheduleUpdates.iterator(); while (updatesIterator.hasNext()) { Intent existingIntent = updatesIterator.next(); if (sessionId.equals(existingIntent.getStringExtra(EXTRA_SESSION_ID))) { updatesIterator.remove(); } } // Queue this schedule update. synchronized (mScheduleUpdates) { mScheduleUpdates.add(intent); } return START_REDELIVER_INTENT; }
public static void main(String[] arg) { ServiceHandler.start(arg); while (true) { /* do nothing, the job is done in background thread */ try { Thread.sleep(1000); } catch (InterruptedException ex) { LOG.fatal("Closing application due to internal error."); System.exit(-1); } } }
@Override public int onStartCommand(Intent intent, int flags, int startId) { // Temporarily removed for this duplicate message track down. mResultCode = intent != null ? intent.getIntExtra("result", 0) : 0; if (mResultCode != 0) { Log.v( TAG, "onStart: #" + startId + " mResultCode: " + mResultCode + " = " + translateResultCode(mResultCode)); } Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); return Service.START_NOT_STICKY; }
public void run() { Looper.prepare(); mServiceHandler = new ServiceHandler(); handlerIsCreated = true; Message msg = mServiceHandler.obtainMessage(); msg.what = OPEN_DATABASE; mServiceHandler.sendMessage(msg); if (needToSaveTransaction) { Message msgSave = mServiceHandler.obtainMessage(); msgSave.what = INSERT_DATA; msgSave.obj = bundle; needToSaveTransaction = false; bundle = null; mServiceHandler.sendMessage(msgSave); } if (callbackObject != null) { callbackObject.onReady(); } Looper.loop(); }
private Message parseIntent(Intent intent) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, -1234); if (DEBUG) { Log.d( TAG, "Action: " + intent.getAction() + " State:" + state + " Device: " + (device == null ? "null" : device.getAliasName())); } int msgType; switch (state) { case Intent.EXTRA_DOCK_STATE_UNDOCKED: msgType = MSG_TYPE_UNDOCKED_TEMPORARY; break; case Intent.EXTRA_DOCK_STATE_DESK: case Intent.EXTRA_DOCK_STATE_HE_DESK: case Intent.EXTRA_DOCK_STATE_CAR: if (device == null) { Log.w(TAG, "device is null"); return null; } /// Fall Through /// case Intent.EXTRA_DOCK_STATE_LE_DESK: if (DockEventReceiver.ACTION_DOCK_SHOW_UI.equals(intent.getAction())) { if (device == null) { Log.w(TAG, "device is null"); return null; } msgType = MSG_TYPE_SHOW_UI; } else { msgType = MSG_TYPE_DOCKED; } break; default: return null; } return mServiceHandler.obtainMessage(msgType, state, 0, device); }