At first, create your post class, using Asynctask or IntentService like following...
public class PostIntentService extends IntentService implements PostTask.Observer {
private int counter = 0;
private int retry = 2;
private Data mData;
public PostIntentService() {
super("PostIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
mData = (Data) intent.getSerializableExtra("data");
// send updating status
Intent i = new Intent();
i.setAction(PostResponseReceiver.ACTION_RESPONSE);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.putExtra("status", "updating");
sendBroadcast(i);
execute();
counter++;
}
@Override
public void onSuccessPost(String result) {
// send success status
Intent i = new Intent();
i.setAction(PostResponseReceiver.ACTION_RESPONSE);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.putExtra("status", "success");
sendBroadcast(i);
}
@Override
public void onFailedPost(String result) {
if (counter < retry) {
execute();
counter++;
}
else {
// send failed status
Intent i = new Intent();
i.setAction(PostResponseReceiver.ACTION_RESPONSE);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.putExtra("status", "failed");
i.putExtra("data", mData);// for reproduct
sendBroadcast(i);
}
}
private void execute() {
PostTask task = new PostTask(this);
task.execute();
}
}
At second, create your class (extended BroadcastReceiver) which receives the intent when a post finished.
public class PostBroadcastReceiver extends BroadcastReceiver {
public static final String ACTION_RESPONSE = "com.example.android.intent.action.POST_PROCESSED";
private static final int POST_REQUEST = 100;
private Observer mObserver;
public PostBroadcastReceiver(Observer observer) {
mObserver = observer;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra("status").equals("updating")) {
}
else if (intent.getStringExtra("status").equals("success")) {
if (mObserver != null) {
mObserver.onPostFinished();
}
}
else if (intent.getStringExtra("status").equals("failed")) {
if (mObserver != null) {
mObserver.onPostFailed();
}
}
}
public interface Observer {
public void onPostFinished();
public void onPostFailed();
}
}
Register this service in your Manifest file.
<service android:name=".PostIntentService" />
Register this reciever in onCreate of your Main Activity.
IntentFilter filter = new IntentFilter(PostBroadcastReceiver.ACTION_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new PostResponseReceiver(this);
registerReceiver(receiver, filter);
And implement following method in your Main activity.
public void onPostFinished() {
Log.d("onPostFinished", "onPostFinished");
}
public void onPostFailed() {
Log.d("onPostFailed", "onPostFailed");
}
Don't forget Unregister this reciever in onDestroy of your Main Activity.
unregisterReceiver(receiver);
Finally, execute the transfer in your login Activity.
Intent intent = new Intent(this, PostIntentService.class);
intent.putExtra("data", mData);
startService(intent);
finish()
the current one to go back. – TranthamHandler
? if not, try once. – Pinto