@@ -14,7 +14,7 @@ have to deal with the nightmare of concurrency. Also, an IntentService is much m
14
14
gets executed in the background and it's completely detached from the UI thread. If you need to show status in your UI,
15
15
read further and you'll discover how to do it very easily.
16
16
17
- ## Installation
17
+ ## Setup
18
18
19
19
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 ) .
20
20
@@ -24,7 +24,11 @@ Add the following to your project's AndroidManifest.xml file:
24
24
<uses-permission android:name="android.permission.INTERNET" />
25
25
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
26
26
27
- And before the tag: <pre ></ application></pre > add the following:
27
+ And before the tag:
28
+
29
+ </ application>
30
+
31
+ add the following:
28
32
29
33
<service
30
34
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
51
55
?>
52
56
53
57
## How to start android upload service to upload files
58
+ For detailed explanation of each parameter, please check JavaDocs.
59
+
54
60
public void upload(final Context context) {
55
61
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
+ */
59
72
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"));
63
76
64
77
//You can add your own custom headers
65
78
request.addHeader("your-custom-header", "your-custom-value");
66
79
80
+ //and parameters
67
81
request.addParameter("parameter-name", "parameter-value");
68
82
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:
70
84
request.addParameter("array-parameter-name", "value1");
71
85
request.addParameter("array-parameter-name", "value2");
72
86
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
82
96
valuesList.add("valueN");
83
97
request.addArrayParameter("array-parameter-name", valuesList);
84
98
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);
92
106
93
107
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
96
109
UploadService.startUpload(request);
110
+
97
111
} catch (Exception exc) {
98
112
//You will end up here only if you pass an incomplete UploadRequest
99
113
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
108
122
109
123
public class YourActivity extends Activity {
110
124
125
+ private static final String TAG = "AndroidUploadService";
126
+
111
127
...
112
128
113
129
private final BroadcastReceiver uploadReceiver = new AbstractUploadServiceReceiver() {
114
130
115
131
@Override
116
132
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);
119
135
}
120
136
121
137
@Override
122
138
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);
125
141
}
126
142
127
143
@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);
131
150
}
132
151
};
133
152
134
153
@Override
135
154
protected void onResume() {
136
155
super.onResume();
137
- final IntentFilter intentFilter = new IntentFilter();
138
- intentFilter.addAction(UploadService.BROADCAST_ACTION);
139
- registerReceiver(uploadReceiver, intentFilter);
156
+ uploadReceiver.register(this);
140
157
}
141
158
142
159
@Override
143
160
protected void onPause() {
144
161
super.onPause();
145
- unregisterReceiver( uploadReceiver);
162
+ uploadReceiver.unregister(this );
146
163
}
147
164
148
165
}
0 commit comments