Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用服务下载文件_Java_Android - Fatal编程技术网

Java 使用服务下载文件

Java 使用服务下载文件,java,android,Java,Android,我正在尝试将下载管理器安装到android,所以当我尝试启动该服务时,它会给我错误信息 在我的清单中,我声明了服务名称,并获得了internet权限和外部存储写入权限 这是主要活动: public class DownloadLabMyClassActivity extends Activity { Button downloadButton; @Override public void onCreate(Bundle savedInstanceState) {

我正在尝试将下载管理器安装到android,所以当我尝试启动该服务时,它会给我错误信息

在我的清单中,我声明了服务名称,并获得了internet权限和外部存储写入权限

这是主要活动:

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

我的问题是:

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

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

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

要将数据传递给服务检查

如何添加一个空构造函数?你不能,cz父类需要传递一个字符串值,我必须删除构造函数中的参数并将null传递给super,这是workyes,但不是。你需要一个空构造函数(这基本上就是你得到的错误)这调用了一个超级函数,它使用字符串作为参数。对,但是如果你传递一个字符串值,它仍然会给你同样的错误,如果你保持它为空,它仍然会给你同样的错误,所以我尝试了null,它被工作编辑了,你将需要超级函数调用中的一个参数。超级函数要求一个名称,这不仅仅是为了看起来漂亮。(事实上是的,但这不是你要知道的。父母要求一个名字,如果不给一个名字,那将是不礼貌的)。