onHandleIntent is not getting called - android

In the App i am developing i am trying to use IntentService as shown below in the code. the IntentService is declared as shown below in the manifest file.
the issue i am facing now is, when I run App the onHandleIntent is never get called.
I checked some examples in the internt but non of them was helpful, because the recommended hints to solve the issue did not work.
I started the service as follows:
this.mButtonFetchURL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mServiceIntent = new Intent(getApplicationContext(), TwitterTrendsAPIService.class);
mServiceIntent.putExtra(CONST_KEY_REQUEST_URL, BASE_REQUEST_URL);
startService(mServiceIntent);
clearEditText(mEditTextURLContents);
}
});
please let me know how to solve it.
code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pc_.twittertrendsnearlocation">
<uses-permission android:name="android.permission.INTERNET" />
<application
....
....
....
<service
android:name=".services.TwitterTrendsAPIService"
android:exported="false"
android:enabled="true"/>
</application>
code
public class TwitterTrendsAPIService extends IntentService {
private static final String TAG = TwitterTrendsAPIService.class.getSimpleName();
private boolean mIsFetching = false;
private String mBaseRequestURL = null;
private RequestQueue mRequestQueue = null;
private JsonArrayRequest mJsonArrayRequest = null;
private final static String CONST_KEY_JSON_ARRAY_TRENDS = "trends";
private JSONObject mEntireJSONObject = null;
private JSONArray mEntireTrendsArray = null;
public TwitterTrendsAPIService() {
super(null);
}
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* #param name Used to name the worker thread, important only for debugging.
*/
public TwitterTrendsAPIService(String name) {
super(name);
}
#Override
public void onCreate() {
super.onCreate();
Log.w(TAG, "[onCreate]");
this.setupVolley();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w(TAG, "[onStartCommand]");
return Service.START_NOT_STICKY;
}
#Override
public void onHandleIntent(Intent intent) {
Log.w(TAG, "[onHandleIntent]");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.w(TAG, "[onDestroy]");
}
private void setupVolley() {
this.mRequestQueue = Volley.newRequestQueue(this);
}
private class ServiceRunnable implements Runnable {
#Override
public void run() {
fetchJSONData();
stopSelf();
}
}
private void fetchJSONData() {
Log.w(TAG, "#fetchJSONData");
this.mJsonArrayRequest = new JsonArrayRequest(Request.Method.GET, this.mBaseRequestURL, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
mEntireJSONObject = response.getJSONObject(0);
mEntireTrendsArray = mEntireJSONObject.getJSONArray(TwitterTrendsAPIService.CONST_KEY_JSON_ARRAY_TRENDS);
Log.i(TAG, "mEntireTrendsArray.length(): " + mEntireTrendsArray.length());
Log.i(TAG, "mEntireTrendsArray.get(0): " + mEntireTrendsArray.get(0));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
}
}

Delete onStartCommand(), or chain to the superclass' implementation of onStartCommand(). Right now, you are overriding the built-in implementation, and IntentService uses that to set up the background thread and call onHandleIntent().

Related

Android using LocalBroadcastManager inside android Service

I am trying to use the LocalBroadcastManager inside an android service.
public class WebSocketService extends Service {
private static final String TAG = "WebSocketService";
private WebSocketClient mWebSocketClient;
public WebSocketService() {
connectWebSocket();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void connectWebSocket() {
URI uri;
try {
uri = new URI("wss://echo.websocket.org");
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
mWebSocketClient = new WebSocketClient(uri) {
#Override
public void onOpen(ServerHandshake serverHandshake) {
Log.i(TAG, "Websocket Opened");
mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
}
#Override
public void onMessage(String message) {
Log.i(TAG, "Websocket Received: " + message);
Intent broadcastIntent = new Intent("custom-event");
broadcastIntent.putExtra("message", message);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(broadcastIntent);
}
#Override
public void onClose(int code, String reason, boolean remote) {
Log.i(TAG, "Websocket closed: " + reason);
}
#Override
public void onError(Exception ex) {
Log.i(TAG, "Websocket error");
}
};
}
public void sendMessage(String message) {
mWebSocketClient.send(message);
}
}
The main Activity where the websocket gets created.
public class MainActivity extends Activity {
private WebSocketService webSocketService;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webSocketService = new WebSocketService();
}
public void send(View view) {
webSocketService.sendMessage("socket message");
}
}
and its xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="send"
tools:layout_editor_absoluteX="166dp"
tools:layout_editor_absoluteY="288dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
The above does no compile with exception
After changing LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent); to
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(broadcastIntent);
I get the error
Error Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
My question is whether its possible to pass the context while creating
mWebSocketClient = new WebSocketClient(uri) {
}
such that the LocalBroadcastManager can be used or not?
The question in general is, is it possible to get the applicationContext inside an annoymous namespace.
To get an outerScope this inside an annoymous method / inner class of any java class you can either call WebSocketService.this
LocalBroadcastManager.getInstance(WebSocketService.this).sendBroadcast(broadcastIntent);
or declare a private helper method for the same
private WebSocketService getOuterWebSocketService() {
return this;
}
Now you can use this method inside onMessage as below
#Override
public void onMessage(String message) {
Log.i(TAG, "Websocket Received: " + message);
Intent broadcastIntent = new Intent("custom-event");
broadcastIntent.putExtra("message", message);
LocalBroadcastManager.getInstance(getOuterWebSocketService()).sendBroadcast(broadcastIntent);
}

How to continue the process of uploading which is started in foreground even after application got killed android?

I am working on project in which there is an requirement of uploading files to server. For uploading big files it will take much time to upload. To avoid this situation I thought of uploading files from background. For uploading files from background I used service inside of that service I am using Async task for uploading files the problem is when uploading got started and if remove the application means killing the service also getting stopped. How to solve this issue. Please find my code snippet below of what I had done up to now
public class BackgroundService extends Service {
ProgressRequestBody.UploadCallbacks callbacks = new ProgressRequestBody.UploadCallbacks() {
#Override
public void onProgressUpdate(int percentage) {
Log.i("percentage", "" + percentage);
}
#Override
public void onError() {
}
#Override
public void onFinish(int finished) {
}
};
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
String filePath = intent.getStringExtra("filePath");
final int studentId = intent.getIntExtra("studentId", 0);
int documentType = intent.getIntExtra("type", 0);
uplodeBackground uplodeBackground = new uplodeBackground();
uplodeBackground.execute(filePath, String.valueOf(documentType), String.valueOf(studentId));
return START_STICKY;
}
class uplodeBackground extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... strings) {
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
File file = new File(strings[0]);
//RequestBody progressRequestBody = RequestBody.create(MediaType.parse("multipart/*"), file);
ProgressRequestBody progressRequestBody = new ProgressRequestBody(file, callbacks);
MultipartBody.Part[] part = new MultipartBody.Part[2];
part[0] = MultipartBody.Part.createFormData("file", file.getName(), progressRequestBody);
part[1] = MultipartBody.Part.createFormData("documentTypeId", strings[1]);
RequestBody mStudendId = RequestBody.create(MediaType.parse("text/plain"), /*String.valueOf(studentId)*/strings[2]);
Call<JsonObject> call = apiInterface.uploadFile(part, mStudendId);
call.enqueue(new Callback<JsonObject>() {
#Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.isSuccessful()) {
if (response.body().get("status").getAsInt() == 200) {
Log.i("test", "Uploaded successfully..........");
}
}
}
#Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Log.i("test", "Failed successfully..........");
}
});
return null;
}
}
}
this is how i am starting service
Intent intent = new Intent(AddVideoFragment.this, BackgroundService.class);
intent.putExtra("filePath", SelectedFile.getPath());
intent.putExtra("studentId", NewRegistartionController.initializeNewRegistrationController().basicDetailsModel.getStudentId());
intent.putExtra("type", doumentTypeId);
intent.putExtra("contentId", objects[1].toString());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
You call your AsyncTask class in forground service.Only forground service not kill when application in background.

Android Pusher Singleton channel subscription

Hello I have implemented pusher for realtime chat and subscribing to pusher channel , but I have many activities and fragments where i want to listen to pushr events . I have added this code in every activity/fragment but the problem is that it creates multiple subscriptions for every id . I know that i have to use Singleton for this can anyone point me in the right direction to achieve this ?
Here is the code i am writing in every activity/fragment
private PusherOptions options;
private Channel channel;
private Pusher pusher;
options = new PusherOptions();
options.setCluster("ap2");
pusher = new Pusher("afbfc1f591fd7b70190f", options);
pusher.connect();
profile_id = Global.shared().preferences.getString("PROFILE_ID", " ");
channel = pusher.subscribe(profile_id);
channel.bind("message",
new SubscriptionEventListener() {
#Override
public void onEvent(String s, String s1, final String data) {
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
JSONObject result = new JSONObject(data);
String message = result.getString("message");
String time = result.getString("time");
String reId = result.getString("recieverId");
new_message = message;
getConvoData(k, message);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("DATA ====>>" + data);
}
});
}
});
okay so after trying for a while i figured it out my self i created a global class and just added pusher code to it so that it maintains just one connection for the entire lifecycle of the app
public class Global extends MultiDexApplication {
#Override
public void onCreate() {
super.onCreate();
SharedPreferences preferences = sharedInstance.getSharedPreferences(sharedInstance.getString(R.string.shared_preferences), Context.MODE_PRIVATE);
sharedInstance.preferences = preferences;
connectTopusher();
}
public void connectTopusher() {
PusherOptions options;
Channel channel;
Pusher pusher;
options = new PusherOptions();
options.setCluster("ap2");
pusher = new Pusher("afbfc1f591fd7b70190f", options);
pusher.connect();
String profile = Global.shared().preferences.getString("PROFILE_ID", "");
channel = pusher.subscribe(profile);
channel.bind("message",
new SubscriptionEventListener() {
#Override
public void onEvent(String s, String s1, final String data) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
try {
JSONObject result = new JSONObject(data);
String message = result.getString("message");
String time = result.getString("time");
String reId = result.getString("recieverId");
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("DATA ====>>" + data);
}
});
}
});
channel.bind("status_change", new SubscriptionEventListener() {
#Override
public void onEvent(String s, String s1, final String data) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
try {
JSONObject result = new JSONObject(data);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("DATA ====>>" + data);
}
});
}
});
}
You can expose channel in your Global class. That will allow you to call bind and unbind in your fragments, when they are in the foreground.
connectToPusher should just create a channel and subscribe to it.
In Global.java:
private Channel channel;
public void connectTopusher() {
PusherOptions options;
Pusher pusher;
options = new PusherOptions();
options.setCluster("ap2");
pusher = new Pusher("afbfc1f591fd7b70190f", options);
pusher.connect();
String profile = Global.shared().preferences.getString("PROFILE_ID", "");
this.channel = pusher.subscribe(profile);
}
public Channel getChannel(){
return this.channel;
}
And then in your activity/fragment you can bind/unbind your listeners to when they are resumed/paused - just keep a reference to it like this:
YourActivity.java (could also be your Fragment)
private SubscriptionEventListener messageListener = new SubscriptionEventListener(){
#Override
public void onEvent(String channel, String event, String data) {
//TODO: do something with events
}
}
//Bind when the listener comes into the foreground:
#Override
protected void onResume() {
super.onResume();
((Global) getActivity().getApplication()).getChannel().bind("message", messageListener);
}
//Make sure to unbind the event listener!
#Override
protected void onPause() {
super.onPause();
((Global) getActivity().getApplication()).getChannel().unbind("message", messageListener);
}
I hope this helps :)

android call same dialog two times

I'm developing an app and I'm blocked in one simple thing.
In my Activity, I show a dialog (AlertDialog.Builder) that ask a mail address and an activation. These two fields are checked with a Rest API.
If the activation code is wrong I restart the activity (with an Intent) and I show again the dialog.
I don't understand why, if I'm wrong the activation code the first time, the second time appears the dialog correctly, but when I click "submit", the app doesn't run the Rest call and return always "Invalid credentials", like if it would remind the old "state".
Instead, if I run the app and I put the correct credentials, all is ok.
Any idea?
Source code:
public class PinActivity extends Activity {
String mail;
String verification;
JSONObject responseServer;
BluetoothSocket bsocket;
ConnectedThreadBT cdbt;
SharedPreferences sharedPref;
SharedPreferences.Editor editor;
EditText mail_add;
EditText verification_code;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check);
setup();
dialogActivation();
}
#Override
public void onDestroy() {
super.onDestroy();
}
private void setup(){
RestClientManager.initialize(getApplicationContext()).enableDebugLog(true);
bsocket = BluetoothApplication.getBSocket();
//salvo codice attivazione sul pacchetto
cdbt=new ConnectedThreadBT(bsocket,mHandler, "PinActivity");
cdbt.start();
}
private void dialogActivation(){
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_dialog_verification, null);
mail_add = (EditText) view.findViewById(R.id.mailAddress);
verification_code = (EditText) view.findViewById(R.id.verification_code);
builder.setView(view).
setPositiveButton(getApplicationContext().getResources().getString(R.string.submit), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//prendo e salvo credenziali
mail = mail_add.getText().toString();
verification = verification_code.getText().toString();
//invio dati al server
activatePPS();
}
});
builder.setCancelable(false);
builder.show();
}
private void activatePPS(){
dialogCheck();
String url = "....";
RestClientManager.getInstance().makeJsonRequest(Request.Method.POST, url, new RequestHandler<>(new RequestCallbacks<JSONObject, Error>()
{
#Override
public void onRequestSuccess(JSONObject response)
{
responseServer = response;
int reply_code = 0;
try {
reply_code = response.getInt("reply_code");
checkReplyCode(reply_code);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onRequestError(Error error)
{
}
}, paramsList()));
}
private void dialogCheck(){
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_dialog_load_check, null);
builder.setView(view);
builder.setCancelable(false);
builder.show();
}
private void checkReplyCode(int reply_code) throws JSONException, IOException {
switch(reply_code){
case 0:
successActivation();
break;
case 1001:
//credenziali invalide
Toast.makeText(getApplicationContext(), getResources().getString(R.string.wrong_credentials), Toast.LENGTH_LONG).show();
Intent intent = new Intent(PinActivity.this, PinActivity.class);
startActivity(intent);
break;
}
}
private void successActivation() throws JSONException {
String access_token = responseServer.get("access_token").toString();
String nickname = responseServer.get(".....
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
int value = sharedPref.getInt("step_conf",0);
if(value==0){
Intent intent = new Intent(getApplicationContext(), MethodCurveActivity.class);
intent.putExtra("style", 0);
startActivity(intent);
}
else{
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
},3000);
}
private ArrayMap<String, String> paramsList(){
ArrayMap<String, String> parameters=new ArrayMap<>();
parameters.put("user_mail", mail);
parameters.put(.....
return parameters;
}
private void resetMobileDevice(){
String url = "....";
RestClientManager.getInstance().makeJsonRequest(Request.Method.POST, url, new RequestHandler<>(new RequestCallbacks<JSONObject, Error>()
{
#Override
public void onRequestSuccess(JSONObject response)
{
System.out.println("Risposta:"+response);
responseServer = response;
int reply_code = 0;
try {
reply_code = response.getInt("reply_code");
} catch (JSONException e) {
e.printStackTrace();
}
try {
checkReplyCode(reply_code);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onRequestError(Error error)
{
}
}, paramsList()));
}
private final Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
}
}
};
}
The important point is in the "case 1001", after error.
I have tried finish() and all the method to delete the old instance of the Activity...
Create Application class in your project and initialize RestClientManager in its onCreate Method like this:
public class MyApp extends Application {
private final static String LOG_TAG = Application.class.getSimpleName();
#Override
public void onCreate() {
Log.d(LOG_TAG, "Application.onCreate - Initializing application...");
super.onCreate();
initializeApplication();
Log.d(LOG_TAG, "Application.onCreate - Application initialized OK");
}
private void initializeApplication() {
RestClientManager.initialize(getApplicationContext()).enableDebugLog(true);
}
}
Add this line in your <Application> tag in androidmanifest.xml file:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:name=".App"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And Make sure your singleton structure should be something like this:
private static RestClientManager instance;
static void initInstance()
{
if (instance == null)
{
// Create the instance
instance = new RestClientManager();
}
}
public static RestClientManager getInstance()
{
// Return the instance
return instance;
}
Remember to remove
RestClientManager.initialize(getApplicationContext()).enableDebugLog(true);
from your main activity.
Please give it a try and let me know.

how to pass data between service and it's application in the right way?

i'm a newbie in android. In my app i create a many-to-many chat, and need to update from server a list of Messages. In order to do so, i created a service that updates every second from the server.
My problem is that i don't know how to pass data back to the application. I know that I should do it using intent and broadcast receiver, but in that I stuck with Bundle object that i have to serialize in order to pass it to the app, and it does not make sense to me, since this operation is not that efficient.
For now i'm using the ref to my application (i think it's not that good but don't know why), and after every update from server in the service i activate the application function, and updates it's fields directly. Moreover i think maybe my code will do some good for beginners as well :)
public class UpdateChatService extends Service {
private static final long DELAY_FOR_CHAT_TASK = 0;
private static final long PERIOD_FOR_CHAT_TASK = 1;
private static final TimeUnit TIME_UNIT_CHAT_TASK = TimeUnit.SECONDS;
//private Task retryTask; TODO: check this out
private ScheduledExecutorService scheduler;
private boolean timerRunning = false;
private long RETRY_TIME = 200000;
private long START_TIME = 5000;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
scheduleChatUpdate();
}
private void scheduleChatUpdate() {
BiggerGameApp app = (BiggerGameApp) getApplication();
this.scheduler = Executors.newScheduledThreadPool(3);
this.scheduler.scheduleAtFixedRate(new UpdateChatTask(app),
DELAY_FOR_CHAT_TASK, PERIOD_FOR_CHAT_TASK,
TIME_UNIT_CHAT_TASK);
timerRunning = true;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!timerRunning) {
scheduleChatUpdate();
}
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
if (scheduler != null) {
scheduler.shutdown();
}
timerRunning = false;
}
}
Here is the code of the asynchronous task the runs in the service.
Please tell me what i'm doing wrong, and how should pass data from the service to the application.
public void run() {
try {
if (this.app.getLastMsgFromServer() == null) {
this.app.setLastMsgFromServer(new Message(new Player(DEFAULT_EMAIL), "", -1));
this.app.getLastMsgFromServer().setMessageId(-1);
}
Gson gson = new GsonBuilder()
.registerTypeAdapter(DateTime.class, new DateTimeTypeConverter())
.create();
ServerHandler serverHandler = new ServerHandler();
String jsonString = gson.toJson(this.app.getLastMsgFromServer());
// Sending player to servlet in server
String resultString = serverHandler.getResultFromServlet(jsonString, "GetListOfMessages");
if (resultString.contains("Error")) {
return;
}
// Parsing answer
JSONObject json = new JSONObject(resultString);
Status status = null;
String statusString = json.getString("status");
if (statusString == null || statusString.length() == 0)
return;
status = Status.valueOf(statusString);
if (Status.SUCCESS.equals(status)) {
ArrayList<Message> tempChat = null;
JSONArray jsonList = json.getJSONArray("data");
MyJsonParser jsonParser = new MyJsonParser();
tempChat = jsonParser.getListOfMessagesFromJson(jsonList.toString());
if (tempChat != null && tempChat.size() != 0) {
// After getting the chat from the server, it saves the last msg
// For next syncing with the server
this.app.setLastMsgFromServer(tempChat.get(LAST_MSG_INDEX));
tempChat.addAll(this.app.getChat());
if (tempChat.size() > SIZE_OF_USER_CHAT) {
tempChat = (ArrayList<Message>) tempChat.subList(0, SIZE_OF_USER_CHAT - 1);
}
this.app.setChat(tempChat);
this.app.updateViews(null);
}
}
return;
Is the Service local only (I'm going to assume "yes")?
Communication with a local-only service can be done by passing an instance of android.os.Binder back, as shown below:
public class UpdateChatService extends Service {
public static final class UpdateChat extends Binder {
UpdateChatService mInstance;
UpdateChat(UpdateChatService instance) {
mInstance = instance;
}
public static UpdateChat asUpdateChat(IBinder binder) {
if (binder instanceof UpdateChat) {
return (UpdateChat) binder;
}
return null;
}
public String pollMessage() {
// Takes a message from the list or returns null
// if the list is empty.
return mInstance.mMessages.poll();
}
public void registerDataSetObserver(DataSetObserver observer) {
mInstance.mObservable.registerObserver(observer);
}
public void unregisterDataSetObserver(DataSetObserver observer) {
mInstance.mObservable.unregisterObserver(observer);
}
}
private ScheduledExecutorService mScheduler;
private LinkedList<String> mMessages;
private DataSetObservable mObservable;
#Override
public IBinder onBind(Intent intent) {
return new UpdateChat(this);
}
#Override
public void onCreate() {
super.onCreate();
mObservable = new DataSetObservable();
mMessages = new LinkedList<String>();
mScheduler = Executors.newScheduledThreadPool(3);
mScheduler.scheduleAtFixedRate(new UpdateChatTask(), 0, 1, TimeUnit.SECONDS);
}
#Override
public void onDestroy() {
super.onDestroy();
mScheduler.shutdownNow();
mObservable.notifyInvalidated();
}
class UpdateChatTask implements Runnable {
int mN = 0;
public void run() {
// This example uses a list to keep all received messages, your requirements may vary.
mMessages.add("Message #" + (++mN));
mObservable.notifyChanged();
}
}
}
This example could be used to feed an Activity (in this case a ListActivity) like this:
public class ChattrActivity extends ListActivity implements ServiceConnection {
LinkedList<String> mMessages;
ArrayAdapter<String> mAdapter;
UpdateChat mUpdateChat;
DataSetObserver mObserver;
Runnable mNotify;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMessages = new LinkedList<String>();
mNotify = new Runnable() {
public void run() {
mAdapter.notifyDataSetChanged();
}
};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mMessages);
getListView().setAdapter(mAdapter);
// Bind to the Service if you do not need it to persist when this Activity
// dies - otherwise you must call #startService(..) before!
bindService(new Intent(this, UpdateChatService.class), this, BIND_AUTO_CREATE);
}
/**
* #see android.app.ListActivity#onDestroy()
*/
#Override
protected void onDestroy() {
super.onDestroy();
if (mUpdateChat != null) {
mUpdateChat.unregisterDataSetObserver(mObserver);
unbindService(this);
}
}
public void onServiceConnected(ComponentName name, IBinder service) {
mUpdateChat = UpdateChat.asUpdateChat(service);
mObserver = new DataSetObserver() {
#Override
public void onChanged() {
String message;
while ((message = mUpdateChat.pollMessage()) != null) {
mMessages.add(message);
}
runOnUiThread(mNotify);
}
#Override
public void onInvalidated() {
// Service was killed - restart or handle this error somehow.
}
};
// We use a DataSetObserver to notify us when a message has been "received".
mUpdateChat.registerDataSetObserver(mObserver);
}
public void onServiceDisconnected(ComponentName name) {
mUpdateChat = null;
}
}
If you need to communicate across processes you should look into implementing an AIDL interface - but for "local" versions this pattern works just fine & doesn't involve abusing the global Application instance.
You can use a static memory shared between your service and rest of application (activities). If you do not plan to expose this service to external apps, then sharing static memory is better than serializing/deserializing data via bundles.
Bundles based approach is encouraged for components that are to be exposed to outside world. A typical app usually has just the primary activity exposed in app manifest file.
If your don't pulibc your service , the static memory and the callback function can do.
If not , you can send broadcast.

Categories

Resources