I was trying to do a service sample program and i am getting following exception

09-10 20:57:57.871: E/AndroidRuntime(280): FATAL EXCEPTION: main 09-10 20:57:57.871: E/AndroidRuntime(280): java.lang.RuntimeException: Unable to instantiate service com.example.demoservice.DownloadService: java.lang.InstantiationException: com.example.demoservice.DownloadService

I have seen many solutions to this type of execption like passing string to constructor etc.But those solutions didnt solved this issue.

Code sample is given below

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

}

Service class

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

}

}

I am running app using Emulator.Is it possible to run this problem in emulator,because writing to external directory

Can anyone help me?

有帮助吗?

解决方案

Use

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

Instead of

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

UPDATE:

  1. You have to declare a default constructor which calls the public IntentService (String name) super constructor of the IntentService class you extend. ie. In simple words, you need to provide no-argument constuctor for your service ,without which android wont be able to instantiate your service.

  2. you start the intentservice using startService(your_intent); And as per the documentation

You should not override onStartCommand() method for your IntentService. Instead, override onHandleIntent(Intent), which the system calls when the IntentService receives a start request.

IntentService

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top