I’m trying to make download manager to android, So when I’m trying to start the service it’s give me errors
In my Manifest I have declare service name and I got internet permission and write on external storage permission
This is the main Activity:
public class DownloadLabMyClassActivity extends Activity {
Button downloadButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
downloadButton = (Button) findViewById(R.id.button1);
downloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent serviceIntent = new Intent(DownloadLabMyClassActivity.this,DownloadService.class);
startService(serviceIntent);
}
});
}
}
This is the service
public class DownloadService extends IntentService {
public DownloadService(String name) {
super("");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent arg0) {
// TODO Auto-generated method stub
Log.d("service", "onHandleIntent()");
/* try {
URL fileUrl = new URL("http://goo.gl/Mfyya");
HttpURLConnection urlConnection = (HttpURLConnection)fileUrl.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File sdcard = Environment.getExternalStorageDirectory();
File file = new File (sdcard, "filename.ext");
FileOutputStream fileoutput = new FileOutputStream (file);
InputStream inputstream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferlength =0;
while ((bufferlength = inputstream.read(buffer)) > 0)
{
fileoutput.write(buffer, 0, bufferlength);
downloadedSize += bufferlength;
//updateProgress(downloadedSize, totalSize);
}
fileoutput.close();
} catch (IOException e) {
Log.d("service", "error");
//e.printStackTrace();
}*/
}
}
and this the error :
11-20 09:38:21.107: E/AndroidRuntime(1627): FATAL EXCEPTION: main
11-20 09:38:21.107: E/AndroidRuntime(1627): java.lang.RuntimeException: Unable to instantiate service com.test2.lab.DownloadService: java.lang.InstantiationException: can't instantiate class com.test2.lab.DownloadService; no empty constructor
11-20 09:38:21.107: E/AndroidRuntime(1627): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2237)
11-20 09:38:21.107: E/AndroidRuntime(1627): at android.app.ActivityThread.access$1600(ActivityThread.java:123)
11-20 09:38:21.107: E/AndroidRuntime(1627): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
11-20 09:38:21.107: E/AndroidRuntime(1627): at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 09:38:21.107: E/AndroidRuntime(1627): at android.os.Looper.loop(Looper.java:137)
11-20 09:38:21.107: E/AndroidRuntime(1627): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-20 09:38:21.107: E/AndroidRuntime(1627): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 09:38:21.107: E/AndroidRuntime(1627): at java.lang.reflect.Method.invoke(Method.java:511)
11-20 09:38:21.107: E/AndroidRuntime(1627): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-20 09:38:21.107: E/AndroidRuntime(1627): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-20 09:38:21.107: E/AndroidRuntime(1627): at dalvik.system.NativeStart.main(Native Method)
11-20 09:38:21.107: E/AndroidRuntime(1627): Caused by: java.lang.InstantiationException: can't instantiate class com.test2.lab.DownloadService; no empty constructor
11-20 09:38:21.107: E/AndroidRuntime(1627): at java.lang.Class.newInstanceImpl(Native Method)
11-20 09:38:21.107: E/AndroidRuntime(1627): at java.lang.Class.newInstance(Class.java:1319)
11-20 09:38:21.107: E/AndroidRuntime(1627): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2234)
11-20 09:38:21.107: E/AndroidRuntime(1627): ... 10 more
My questions :
- How to start this service correctly?
- The needed permission and if anything missing?
- How to download multiple files using this service?
- Can I instantiate this service for every file and add it to a list of them, so in the future I can pause or resume the whole list or specific service?
You need to use empty constructor of the service, the system instantiate it for you..
To pass data to the service check Pass data from Activity to Service using an Intent