Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 853fe35

Browse files
committed
Added utility method to register and unregister the broadcast receiver.
Updated README.MD
1 parent 81131c6 commit 853fe35

File tree

2 files changed

+82
-39
lines changed

2 files changed

+82
-39
lines changed

README.md

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ have to deal with the nightmare of concurrency. Also, an IntentService is much m
1414
gets executed in the background and it's completely detached from the UI thread. If you need to show status in your UI,
1515
read further and you'll discover how to do it very easily.
1616

17-
## Installation
17+
## Setup
1818

1919
Check out the project and add android-upload-service to your project as an [Android Library Project](http://developer.android.com/guide/developing/projects/projects-eclipse.html#ReferencingLibraryProject).
2020

@@ -24,7 +24,11 @@ Add the following to your project's AndroidManifest.xml file:
2424
<uses-permission android:name="android.permission.INTERNET" />
2525
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2626

27-
And before the tag: <pre></ application></pre> add the following:
27+
And before the tag:
28+
29+
</ application>
30+
31+
add the following:
2832

2933
<service
3034
android:name="com.alexbbb.uploadservice.UploadService"
@@ -51,22 +55,32 @@ Upload it to your server and pass "uploaded_file" as the second parameter to the
5155
?>
5256

5357
## How to start android upload service to upload files
58+
For detailed explanation of each parameter, please check JavaDocs.
59+
5460
public void upload(final Context context) {
5561
final UploadRequest request = new UploadRequest(context,
56-
"custom-upload-id", //This is used when receiving upload status
57-
"http://www.yoursite.com/your/script");
58-
62+
"custom-upload-id",
63+
"http://www.yoursite.com/yourscript");
64+
65+
/*
66+
* parameter-name: is the name of the parameter that will contain file's data.
67+
* Pass "uploaded_file" if you're using the test PHP script
68+
*
69+
* custom-file-name.extension: is the file name seen by the server.
70+
* E.g. value of $_FILES["uploaded_file"]["name"] of the test PHP script
71+
*/
5972
request.addFileToUpload("/absolute/path/to/your/file",
60-
"parameter-name", //Name of the parameter that will contain file's data. Pass "uploaded_file" if you're using the test PHP script
61-
"custom-file-name.extension", //File name seen by the server. E.g. value of $_FILES["uploaded_file"]["name"] of the test PHP script
62-
"content-type")); //You can find many common content types defined as static constants in the ContentType class
73+
"parameter-name",
74+
"custom-file-name.extension",
75+
"content-type"));
6376

6477
//You can add your own custom headers
6578
request.addHeader("your-custom-header", "your-custom-value");
6679

80+
//and parameters
6781
request.addParameter("parameter-name", "parameter-value");
6882
69-
//If you want to add an array of strings, you can do the following:
83+
//If you want to add a parameter with multiple values, you can do the following:
7084
request.addParameter("array-parameter-name", "value1");
7185
request.addParameter("array-parameter-name", "value2");
7286
request.addParameter("array-parameter-name", "valueN");
@@ -82,18 +96,18 @@ Upload it to your server and pass "uploaded_file" as the second parameter to the
8296
valuesList.add("valueN");
8397
request.addArrayParameter("array-parameter-name", valuesList);
8498

85-
request.setNotificationConfig(
86-
android.R.drawable.ic_menu_upload, //Notification icon. You can use your own app's R.drawable.your_resource
87-
"notification title", //You can use your string resource with: context.getString(R.string.your_string)
88-
"upload in progress text",
89-
"upload completed successfully text",
90-
"upload error text",
91-
false); //Set this to true if you want the notification to be automatically cleared when upload is successful
99+
//configure the notification
100+
request.setNotificationConfig(android.R.drawable.ic_menu_upload,
101+
"notification title",
102+
"upload in progress text",
103+
"upload completed successfully text"
104+
"upload error text",
105+
false);
92106
93107
try {
94-
//Utility method that creates the intent and starts the upload service in the background
95-
//As soon as the service starts, you'll see upload status in Android Notification Center :)
108+
//Start upload service and display the notification
96109
UploadService.startUpload(request);
110+
97111
} catch (Exception exc) {
98112
//You will end up here only if you pass an incomplete UploadRequest
99113
Log.e("AndroidUploadService", exc.getLocalizedMessage(), exc);
@@ -108,41 +122,44 @@ So to listen for the status of the upload service in an Activity for example, yo
108122

109123
public class YourActivity extends Activity {
110124

125+
private static final String TAG = "AndroidUploadService";
126+
111127
...
112128
113129
private final BroadcastReceiver uploadReceiver = new AbstractUploadServiceReceiver() {
114130

115131
@Override
116132
public void onProgress(String uploadId, int progress) {
117-
Log.i("AndroidUploadService", "The progress of the upload with ID " + uploadId
118-
+ " is: " + progress);
133+
Log.i(TAG, "The progress of the upload with ID "
134+
+ uploadId + " is: " + progress);
119135
}
120136

121137
@Override
122138
public void onError(String uploadId, Exception exception) {
123-
Log.e("AndroidUploadService", "Error in upload with ID: " + uploadId + ". "
124-
+ exception.getLocalizedMessage(), exception);
139+
Log.e(TAG, "Error in upload with ID: " + uploadId + ". "
140+
+ exception.getLocalizedMessage(), exception);
125141
}
126142

127143
@Override
128-
public void onCompleted(String uploadId, int serverResponseCode, String serverResponseMessage) {
129-
Log.i("AndroidUploadService", "Upload with ID " + uploadId + " is completed: "
130-
+ serverResponseCode + ", " + serverResponseMessage);
144+
public void onCompleted(String uploadId,
145+
int serverResponseCode,
146+
String serverResponseMessage) {
147+
Log.i(TAG, "Upload with ID " + uploadId
148+
+ " is completed: " + serverResponseCode
149+
+ ", " + serverResponseMessage);
131150
}
132151
};
133152
134153
@Override
135154
protected void onResume() {
136155
super.onResume();
137-
final IntentFilter intentFilter = new IntentFilter();
138-
intentFilter.addAction(UploadService.BROADCAST_ACTION);
139-
registerReceiver(uploadReceiver, intentFilter);
156+
uploadReceiver.register(this);
140157
}
141158
142159
@Override
143160
protected void onPause() {
144161
super.onPause();
145-
unregisterReceiver(uploadReceiver);
162+
uploadReceiver.unregister(this);
146163
}
147164

148165
}

src/com/alexbbb/uploadservice/AbstractUploadServiceReceiver.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package com.alexbbb.uploadservice;
22

3+
import android.app.Activity;
34
import android.content.BroadcastReceiver;
45
import android.content.Context;
56
import android.content.Intent;
7+
import android.content.IntentFilter;
68

79
/**
8-
* Abstract broadcast receiver from which to inherit when creating a receiver
9-
* for {@link UploadService}.
10-
*
11-
* It provides the boilerplate code to properly handle broadcast messages coming
12-
* from the upload service and dispatch them to the proper handler method.
13-
*
10+
* Abstract broadcast receiver from which to inherit when creating a receiver for {@link UploadService}.
11+
*
12+
* It provides the boilerplate code to properly handle broadcast messages coming from the upload service and dispatch
13+
* them to the proper handler method.
14+
*
1415
* @author alexbbb (Alex Gotev)
1516
* @author eliasnaur
16-
*
17+
*
1718
*/
1819
public abstract class AbstractUploadServiceReceiver extends BroadcastReceiver {
1920

@@ -23,11 +24,12 @@ public void onReceive(Context context, Intent intent) {
2324
if (intent != null) {
2425
if (UploadService.BROADCAST_ACTION.equals(intent.getAction())) {
2526
final int status = intent.getIntExtra(UploadService.STATUS, 0);
26-
final String uploadId = intent.getStringExtra(UploadService.UPLOAD_ID);
27+
final String uploadId = intent.getStringExtra(UploadService.UPLOAD_ID);
2728

2829
switch (status) {
2930
case UploadService.STATUS_ERROR:
30-
final Exception exception = (Exception) intent.getSerializableExtra(UploadService.ERROR_EXCEPTION);
31+
final Exception exception = (Exception) intent
32+
.getSerializableExtra(UploadService.ERROR_EXCEPTION);
3133
onError(uploadId, exception);
3234
break;
3335

@@ -50,25 +52,49 @@ public void onReceive(Context context, Intent intent) {
5052

5153
}
5254

55+
/**
56+
* Register this upload receiver. It's recommended to register the receiver in Activity's onResume method.
57+
*
58+
* @param activity activity in which to register this receiver
59+
*/
60+
public void register(final Activity activity) {
61+
final IntentFilter intentFilter = new IntentFilter();
62+
intentFilter.addAction(UploadService.BROADCAST_ACTION);
63+
activity.registerReceiver(this, intentFilter);
64+
}
65+
66+
/**
67+
* Unregister this upload receiver. It's recommended to unregister the receiver in Activity's onPause method.
68+
*
69+
* @param activity activity in which to unregister this receiver
70+
*/
71+
public void unregister(final Activity activity) {
72+
activity.unregisterReceiver(this);
73+
}
74+
5375
/**
5476
* Called when the upload progress changes.
77+
*
5578
* @param uploadId unique ID of the upload request
5679
* @param progress value from 0 to 100
5780
*/
5881
public abstract void onProgress(final String uploadId, final int progress);
5982

6083
/**
6184
* Called when an error happens during the upload.
85+
*
6286
* @param uploadId unique ID of the upload request
6387
* @param exception exception that caused the error
6488
*/
6589
public abstract void onError(final String uploadId, final Exception exception);
6690

6791
/**
6892
* Called when the upload is completed successfully.
93+
*
6994
* @param uploadId unique ID of the upload request
7095
* @param serverResponseCode status code returned by the server
7196
* @param serverResponseMessage string containing the response received from the server
7297
*/
73-
public abstract void onCompleted(final String uploadId, final int serverResponseCode, final String serverResponseMessage);
98+
public abstract void onCompleted(final String uploadId, final int serverResponseCode,
99+
final String serverResponseMessage);
74100
}

0 commit comments

Comments
 (0)