下载文件使用服务

时间:2012-11-20 08:39:45

标签: java android

我正在尝试将下载管理器设置为android,所以当我尝试启动该服务时,它会给我错误

在我的清单中,我已声明服务名称,并获得了互联网许可,并在外部存储许可上写下

这是主要活动:

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);
            }
        });
}
}

这是服务

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();
        }*/
    }
}

这就是错误:

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

我的问题:

  • 如何正确启动此服务?
  • 需要的许可,如有遗漏?
  • 如何使用此服务下载多个文件?
  • 我可以为每个文件实例化此服务并将其添加到它们的列表中,以便将来我可以暂停或恢复整个列表或特定服务吗?

1 个答案:

答案 0 :(得分:1)

您需要使用服务的空构造函数,系统会为您实例化它。

public DownloadService() {
    super("DownloadService");
    // TODO Auto-generated constructor stub
}

将数据传递给服务检查Pass data from Activity to Service using an Intent