下面列出了android.os.Message#obtain ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void quitSynchronously() {
state = State.DONE;
cameraManager.stopPreview();
Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
quit.sendToTarget();
try {
// Wait at most half a second; should be enough time, and onPause()
// will timeout quickly
decodeThread.join(500L);
} catch (InterruptedException e) {
// continue
}
// Be absolutely sure we don't send any queued up messages
removeMessages(R.id.decode_succeeded);
removeMessages(R.id.decode_failed);
}
public static void sendStatisticsInfo(int connect_time) {
CarlifeCmdMessage command = new CarlifeCmdMessage(true);
command.setServiceType(CommonParams.MSG_CMD_STATISTIC_INFO);
CarlifeStatisticsInfo.Builder builder = CarlifeStatisticsInfo.newBuilder();
builder.setCuid(CarlifeUtil.getInstance().getCuid());
builder.setVersionName(CarlifeUtil.getInstance().getVersionName());
builder.setVersionCode(CarlifeUtil.getInstance().getVersionCode());
builder.setChannel(CarlifeUtil.getInstance().getChannel());
builder.setConnectCount(PreferenceUtil.getInstance().getInt(CommonParams.CARLIFE_CONNECT_COUNT, 1));
builder.setConnectSuccessCount(1);
builder.setConnectTime(connect_time);
String crashLog = CrashHandler.getInstance().readCrashInfoFromDir();
if (crashLog != null) {
LogUtil.d(TAG, crashLog);
builder.setCrashLog(crashLog);
}
CarlifeStatisticsInfo statisInfo = builder.build();
command.setData(statisInfo.toByteArray());
command.setLength(statisInfo.getSerializedSize());
Message msgTmp =
Message.obtain(null, command.getServiceType(), CommonParams.MSG_CMD_PROTOCOL_VERSION, 0, command);
ConnectClient.getInstance().sendMsgToService(msgTmp);
PreferenceUtil.getInstance().putInt(CommonParams.CARLIFE_CONNECT_COUNT, 0);
}
public void onClick(View v) {
Message m = null;
if (v == mButtonPositive && mButtonPositiveMessage != null) {
m = Message.obtain(mButtonPositiveMessage);
} else if (v == mButtonNegative && mButtonNegativeMessage != null) {
m = Message.obtain(mButtonNegativeMessage);
} else if (v == mButtonNeutral && mButtonNeutralMessage != null) {
m = Message.obtain(mButtonNeutralMessage);
}
if (m != null) {
m.sendToTarget();
}
if (mAutoDismiss) {
// Post a message so we dismiss after the above handlers are
// executed
mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialogInterface)
.sendToTarget();
}
}
public void quitSynchronously() {
state = State.DONE;
CameraManager.get().stopPreview();
Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
quit.sendToTarget();
try {
decodeThread.join();
} catch (InterruptedException e) {
// continue
}
// Be absolutely sure we don't send any queued up messages
removeMessages(R.id.decode_succeeded);
removeMessages(R.id.decode_failed);
}
private Message getMsg(int what, int arg1, int arg2, Object obj){
Message msg = Message.obtain();
msg.what = what;
msg.arg1 = arg1;
msg.arg2 = arg2;
msg.obj = obj;
return msg;
}
public void onServiceConnected(final ComponentName className, final IBinder service) {
if (D) Log.d(TAG, "mServiceMainConnection : onServiceConnected()");
mServiceMainMessenger = new Messenger(service);
try {
Message message = Message.obtain(null, CBIServiceMain.BOUND_MSG_REGISTER_CLIENT);
message.replyTo = mServiceMainIncomingMessenger;
mServiceMainMessenger.send(message);
} catch (RemoteException e) {
serviceMainLostBinding();
}
}
public static <T extends RemoteEventBean> Message obtainEventMsg(@NonNull T event) {
Message message = Message.obtain();// new Message();
message.what = WHAT_SEND_CROSS_PROCESS_EVENT;
Bundle bundle = new Bundle();
bundle.putString(BUNDLE_STR_EVENT_CLZ, event.getClass().getName());
bundle.putParcelable(BUNDLE_PARCEL_EVENT, event);
message.setData(bundle);
return message;
}
private void sendRequest(int what, Bundle data, Messenger cbMessenger) throws RemoteException {
Message msg = Message.obtain();
msg.what = what;
msg.arg1 = 1;
msg.setData(data);
msg.replyTo = cbMessenger;
this.mMessenger.send(msg);
}
private void sendBinary2Activity(int type, int arg, byte[] raw) {
try {
Message msg = Message.obtain(null, type, raw.length, arg);
ServiceFactory.setDataToMessage(msg, raw);
outbox.send(msg);
} catch (Exception e) {
}
}
public void quitSynchronously() {
state = State.DONE;
CameraManager.get().stopPreview();
Message quit = Message.obtain(decodeThread.getHandler(), Cons.ID_QUIT);
quit.sendToTarget();
try {
decodeThread.join();
} catch (InterruptedException e) {
// continue
}
// Be absolutely sure we don't send any queued up messages
removeMessages(Cons.ID_DECODE_SUCCEED);
removeMessages(Cons.ID_DECODE_FAILED);
}
void handleSetDevice(String macAddress) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(MainActivity.PREFS_DEFAULT_MAC_ADDR, macAddress);
if(macAddress.isEmpty()) {
if(mState != STATUS_DISCONNECTED) {
mScreenshotService.unsync();
mWeatherService.unsync();
mNotificationService.unsync();
mMediaService.unsync();
mTimeService.unsync();
mDevice.disconnect();
mDevice.unbond();
}
mDevice = null;
editor.putString(MainActivity.PREFS_DEFAULT_LOC_NAME, "");
} else {
mDevice = mBleMngr.getDevice(macAddress);
String name = mDevice.getName_normalized();
try {
Message answer = Message.obtain(null, MSG_SET_LOCAL_NAME);
answer.obj = name;
replyTo.send(answer);
replyTo.send(Message.obtain(null, MSG_SET_STATUS, mState, 0));
} catch (RemoteException | NullPointerException ignored) {}
editor.putString(MainActivity.PREFS_DEFAULT_LOC_NAME, name);
}
editor.apply();
}
private void sendConnectMessageIfPossible(Intent intent) {
if (intent.hasExtra(EXTRA_STRING_IP)) {
final String ip = intent.getStringExtra(EXTRA_STRING_IP);
final int port = intent.getIntExtra(EXTRA_INT_PORT, 0);
final int auth = intent.getIntExtra(EXTRA_INT_AUTH, 0);
Message msg = Message.obtain();
msg.obj = ClementineMessageFactory
.buildConnectMessage(ip, port, auth, true, false);
App.ClementineConnection.mHandler.sendMessage(msg);
}
}
@Override
public void onMessage(WebSocket webSocket, String text) {
Log.i("KCV", text);
ChatMsgObject chatMsgObject = gson.fromJson(text, ChatMsgObject.class);
if(chatMsgObject != null) {
Message message = Message.obtain();
message.obj = chatMsgObject;
mWebSocketHandler.sendMessage(message);
}
}
@SuppressWarnings("unused")
private void pingClients(){
Message message = Message.obtain();
Log.d(TAG, "Pinging "+ registeredApps.size()+ " clients");
int result;
synchronized(REGISTERED_APPS_LOCK){
Collection<RegisteredApp> apps = registeredApps.values();
Iterator<RegisteredApp> it = apps.iterator();
while(it.hasNext()){
RegisteredApp app = it.next();
result = app.sendMessage(message);
if(result == RegisteredApp.SEND_MESSAGE_ERROR_MESSENGER_DEAD_OBJECT){
app.close();
Vector<Long> sessions = app.getSessionIds();
for(Long session:sessions){
if(session !=null && session != -1){
List<TransportType> transportTypes = app.getTransportsForSession(session.intValue());
if(transportTypes != null && transportTypes.size() > 0){
attemptToCleanUpModule(session.intValue(), cachedModuleVersion, transportTypes.get(0) );
}
}
}
it.remove();
}
}
}
}
void onCompleted(@NonNull String jobId,
@NonNull final List<TrackTransformationInfo> trackTransformationInfos) {
futureMap.remove(jobId);
if (handler == null) {
listener.onCompleted(jobId, trackTransformationInfos);
} else {
Message msg = Message.obtain(handler, EVENT_COMPLETED);
msg.obj = trackTransformationInfos;
data.putString(KEY_JOB_ID, jobId);
msg.setData(data);
msg.sendToTarget();
}
}
public void startLocation() {
try {
Message obtain = Message.obtain();
obtain.arg1 = 3;
this.a.sendMessage(obtain);
} catch (Throwable th) {
th.printStackTrace();
}
}
void deleteAll() {
final Message m = Message.obtain();
m.what = DELETE_ALL;
mWorker.runMessage(m);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
int currentVolume = App.Clementine.getVolume();
// Control the volume of clementine if enabled in the options
if (mSharedPref.getBoolean(SharedPreferencesKeys.SP_KEY_USE_VOLUMEKEYS, true)) {
int volumeInc = Integer.parseInt(
mSharedPref.getString(SharedPreferencesKeys.SP_VOLUME_INC,
Clementine.DefaultVolumeInc));
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
Message msgDown = Message.obtain();
msgDown.obj = ClementineMessageFactory
.buildVolumeMessage(App.Clementine.getVolume() - volumeInc);
App.ClementineConnection.mHandler.sendMessage(msgDown);
if (currentVolume >= volumeInc) {
currentVolume -= volumeInc;
} else {
currentVolume = 0;
}
makeToast(getString(R.string.playler_volume) + " " + currentVolume + "%",
Toast.LENGTH_SHORT);
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
Message msgUp = Message.obtain();
msgUp.obj = ClementineMessageFactory
.buildVolumeMessage(App.Clementine.getVolume() + volumeInc);
App.ClementineConnection.mHandler.sendMessage(msgUp);
if ((currentVolume + volumeInc) >= 100) {
currentVolume = 100;
} else {
currentVolume += volumeInc;
}
makeToast(getString(R.string.playler_volume) + " " + currentVolume + "%",
Toast.LENGTH_SHORT);
return true;
default:
break;
}
}
}
return super.onKeyDown(keyCode, event);
}
private void sendMessage(int what, Object obj) {
Message msg = Message.obtain();
msg.what = what;
msg.obj = obj;
mH.sendMessage(msg);
}
@Override
protected void onPostExecute(AuthResult authResult) {
Message message = Message.obtain();
message.obj = authResult;
callback.handleMessage(message);
}