Java tutorial
/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.alerts.activity; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.widget.ImageView; import com.liferay.alerts.R; import com.liferay.alerts.model.Alert; import com.liferay.alerts.model.AlertType; import com.liferay.alerts.model.User; import com.liferay.alerts.test.AlertFactory; import com.liferay.alerts.util.PortraitUtil; import com.liferay.alerts.util.SettingsUtil; import com.liferay.alerts.util.ToastUtil; import com.liferay.alerts.util.Validator; import com.liferay.alerts.widget.EditText; import com.liferay.alerts.widget.EditText.KeyboardHideListener; import com.liferay.alerts.widget.TextView; import com.liferay.mobile.android.service.Session; import com.liferay.mobile.android.task.callback.AsyncTaskCallback; import com.liferay.mobile.android.task.callback.typed.JSONObjectAsyncTaskCallback; import com.liferay.mobile.android.v62.pushnotificationsentry.PushnotificationsentryService; import org.json.JSONObject; /** * @author Bruno Farache */ public class ComposeActivity extends Activity implements OnClickListener { @Override public void onClick(View view) { String message = _edit.getText().toString(); if (Validator.isNull(message)) { return; } AsyncTaskCallback callback = new JSONObjectAsyncTaskCallback() { @Override public void onFailure(Exception exception) { _handleException(exception); } @Override public void onSuccess(JSONObject result) { ToastUtil.show(ComposeActivity.this, R.string.post_success, true); finish(); } }; Session session = SettingsUtil.getSession(callback); PushnotificationsentryService service = new PushnotificationsentryService(session); try { JSONObject payload = new JSONObject(); payload.put(Alert.MESSAGE, message); payload.put(Alert.TYPE, AlertType.TEXT.toString()); service.addPushNotificationsEntry(payload.toString()); } catch (Exception e) { _handleException(e); } } @Override public void onCreate(Bundle state) { super.onCreate(state); setContentView(R.layout.compose); _edit = (EditText) findViewById(R.id.edit); ImageView post = (ImageView) findViewById(R.id.post); post.setOnClickListener(this); setBackListener(); User user = AlertFactory.getZeno(this); ImageView portrait = (ImageView) findViewById(R.id.portrait); TextView userName = (TextView) findViewById(R.id.user_name); PortraitUtil.setUserDetails(this, user, portrait, userName); showKeyboard(); } protected void setBackListener() { View navigator = findViewById(R.id.navigator); navigator.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { finish(); } }); } protected void showKeyboard() { final InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); Runnable runnable = new Runnable() { @Override public void run() { manager.showSoftInput(_edit, InputMethodManager.SHOW_IMPLICIT); } }; int delay = getResources().getInteger(R.integer.compose_show_keyboard_delay); Handler handler = new Handler(); handler.postDelayed(runnable, delay); _edit.setKeyboardHideListener(new KeyboardHideListener() { @Override public void onHide() { finish(); } }); } private void _handleException(Exception exception) { String message = getString(R.string.post_failure); message = message + exception.getMessage(); ToastUtil.show(this, message, true); } private EditText _edit; }