Created
January 16, 2013 21:30
-
-
Save swarut/4551127 to your computer and use it in GitHub Desktop.
Android: BroadcastReceiver Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package th.co.fingertip.eventproFP.ui; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import th.co.fingertip.eventproFP.EventproFPEnum; | |
import android.app.Activity; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.net.Uri; | |
import android.os.Bundle; | |
public class EventMapActivity extends MapActivity implements View.OnClickListener { | |
/* | |
* BraodcastReceiver instance lets you catch the message that is broadcasted somewhere. | |
* When the message is received, the onReceive() method of BoardcastReceiver instance | |
* will be trigger if it's registered to the activity. On activity may have more than | |
* one receiver. The example of boardcast receiver is shown below. | |
* | |
* Steps to add BoardcardReceiver: | |
* 1. declare a variable as BoardcastReceiver type using anonymous class declarion pattern. | |
* 2. within the anonymous class scope, override onReceive() | |
* 3. implement what you want to do (after the message is received) in onReceive() method. | |
* 4. register the receiver instance to the activity in onResume() method. | |
* 5. unregister the receiver instance from the activity in onPause() method. | |
* | |
*/ | |
public BroadcastReceiver event_detail_receiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
try { | |
// do something after receiving the message | |
} | |
catch(Exception e){ | |
} | |
} | |
}; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.event_map_layout); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
unregisterReceiver(event_detail_receiver); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
/* register event_detail_receiver to the activity. | |
* the second parameter is the message (intent) that we want to catch | |
* with this receiver instance. | |
*/ | |
registerReceiver(event_detail_receiver, new IntentFilter(EventproFPEnum.Action.VIEW_EVENT)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package th.co.fingertip.eventproFP.http; | |
import java.io.IOException; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.StatusLine; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.HttpResponseException; | |
import org.apache.http.client.ResponseHandler; | |
import org.apache.http.client.methods.HttpUriRequest; | |
import org.apache.http.impl.client.BasicResponseHandler; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.util.EntityUtils; | |
import th.co.fingertip.eventproFP.EventproFPEnum; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.AsyncTask; | |
/** | |
* @author raisonde | |
* | |
*/ | |
public class RestTask extends AsyncTask<HttpUriRequest, Void, String>{ | |
public static final String HTTP_DEFAULT_RESPONSE = "httpResponse"; | |
public static final String HTTP_SEARCH_EVENT_RESPONSE = "httpEventResponse"; | |
public static final String HTTP_SEARCH_CATEGORY_RESPONSE = "httpCategoryResponse"; | |
private Context context; | |
private String action; | |
private HttpClient http_client; | |
ResponseHandler<String> response_handler = new ResponseHandler<String>() { | |
public String handleResponse(final HttpResponse response) | |
throws HttpResponseException, IOException { | |
StatusLine statusLine = response.getStatusLine(); | |
if (statusLine.getStatusCode() >= 300) { | |
throw new HttpResponseException(statusLine.getStatusCode(), | |
statusLine.getReasonPhrase()); | |
} | |
HttpEntity entity = response.getEntity(); | |
return entity == null ? null : EntityUtils.toString(entity, EventproFPEnum.Encoding.UTF); | |
} | |
}; | |
public RestTask(Context context, String action) { | |
super(); | |
this.context = context; | |
this.action = action; | |
this.http_client = new DefaultHttpClient(); | |
} | |
public RestTask(Context context, String action, HttpClient http_client) { | |
super(); | |
this.context = context; | |
this.action = action; | |
this.http_client = http_client; | |
} | |
@Override | |
protected String doInBackground(HttpUriRequest... params) { | |
try{ | |
//create request | |
HttpUriRequest http_request = params[0]; | |
//execute request | |
HttpResponse http_response = http_client.execute(http_request); | |
String response_string = response_handler.handleResponse(http_response); | |
return response_string; | |
} | |
catch(Exception e){ | |
return null; | |
} | |
} | |
//callback that broadcasts the message when the htttp response is returned. | |
@Override | |
protected void onPostExecute(String result) { | |
Intent intent = new Intent(action); | |
if (action.equals(EventproFPEnum.Action.VIEW_EVENT)) { | |
intent.putExtra(HTTP_SEARCH_EVENT_RESPONSE, result); | |
} | |
/* | |
* Fire the message to the receivers that are registered in context(activity) instance | |
* with an intent as a message. | |
*/ | |
context.sendBroadcast(intent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment