使用Service下载时发生InstantiationException

时间:2013-09-10 15:34:08

标签: android service exception-handling instantiationexception

我正在尝试做一个服务示例程序,我正在跟踪异常

  

09-10 20:57:57.871:E / AndroidRuntime(280):致命异常:主要09-10   20:57:57.871:E / AndroidRuntime(280):java.lang.RuntimeException:   无法实例化服务com.example.demoservice.DownloadService:   java.lang.InstantiationException:   com.example.demoservice.DownloadService

我已经看到了很多解决这种类型的execption的方法,比如将字符串传递给构造函数等。但这些解决方案并没有解决这个问题。

代码示例如下所示

public class MainActivity extends Activity {

TextView     textView ;
private BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Bundle bundle = intent.getExtras();
        if(bundle != null){
            String filepath = bundle.getString(DownloadService.FILEPATH);
            int result = bundle.getInt(DownloadService.RESULT);
            if(result == Activity.RESULT_OK){
                Toast.makeText(context, "Sucess" + filepath, Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(context, "Sucess", Toast.LENGTH_SHORT).show();

            }
        }

    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.status);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public void onClick(View v){
    Intent i = new Intent(this, DownloadService.class);
    i.putExtra(DownloadService.FILENAME, "index.html");
    i.putExtra(DownloadService.URL, "http://www.vogella.com/index.html");
    startService(i);
    textView.setText("Service started");
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    registerReceiver(receiver, new IntentFilter(DownloadService.NOTIFICATION));
}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    unregisterReceiver(receiver);
}

}

服务类

public class DownloadService extends IntentService{
  private int result = Activity.RESULT_CANCELED;
  public static final String URL = "urlpath";
  public static final String FILENAME = "filename";
  public static final String FILEPATH = "filepath";
  public static final String RESULT = "result";
  public static final String NOTIFICATION = "com.vogella.android.service.receiver";

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

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    String urlpath = intent.getStringExtra(URL);
    String filename = intent.getStringExtra(urlpath);
    File output = new File(Environment.getExternalStorageDirectory(), filename);
    if(output.exists()){
        output.delete();
    }
    InputStream input = null;
    FileOutputStream fout = null;

    try {
        java.net.URL url = new java.net.URL(urlpath);
        input = url.openConnection().getInputStream();
        InputStreamReader reader = new InputStreamReader(input);
        fout = new FileOutputStream(output.getPath());
        int next = -1;
        while((next = reader.read())!= -1){
            fout.write(next);
        }
        result = Activity.RESULT_OK;
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally{
        if(input != null){
            try {
                input.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(fout != null){
            try {
                fout.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    publishResult( output.getAbsoluteFile(), result);
}

private void publishResult(File absoluteFile, int result2) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(this,DownloadService.class);
    intent.putExtra(FILEPATH, absoluteFile);
    intent.putExtra(RESULT, result2);
    sendBroadcast(intent);

}

}

我正在使用Emulator运行应用程序。是否有可能在模拟器中运行此问题,因为写入外部目录

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

使用

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

而不是

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

<强>更新

  1. 您必须声明一个默认构造函数,它会调用您扩展的public IntentService (String name)类的IntentService超级构造函数。 ie。简单来说,您需要为您的服务提供无参数的构造函数,否则android将无法实例化您的服务。

  2. 使用startService(your_intent);启动intentservice并根据文档

  3.   

    您不应该覆盖onStartCommand()方法   IntentService。相反,覆盖onHandleIntent(Intent),其中   当IntentService收到启动请求时系统调用。

    IntentService