gpt4 book ai didi

android - 使用服务下载时发生 InstantiationException

转载 作者:行者123 更新时间:2023-11-29 01:50:05 25 4
gpt4 key购买 nike

我正在尝试做一个服务示例程序,但出现以下异常

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

我见过很多这种类型的执行的解决方案,比如将字符串传递给构造函数等。但是这些解决方案并没有解决这个问题。

代码示例如下

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

}

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

谁能帮帮我?

最佳答案

使用

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

代替

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

更新:

  1. 您必须声明一个默认构造函数,它调用IntentService 类的public IntentService (String name) super 构造函数延长。 即。简而言之,您需要为您的服务提供无参数的构造函数,否则 android 将无法实例化您的服务。

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

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

关于android - 使用服务下载时发生 InstantiationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18723138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com