问题
I'm trying to start a background service from a pop-up dialog and it's just doesn't work for me
this is the code for opening the dialog box:
reportWrongLang.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
ReportWrongLangDialog Dialog = new ReportWrongLangDialog(imageInfo.getParam("imageId")[0], getApplicationContext());
Dialog.show(fm, "are_you_sure_dialog");
}
in the ReportWrongLangDialog i am saving the appContext, and the imageId
and in the dialog when pressing the report button I want to start the background service that will report about the image
the code for the onClick
report.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("got imageid: " + imageId);
Intent intent = new Intent(appContext, ReportImageService.class);
intent.putExtra("ReportType", "IMAGE_REPORT");
intent.putExtra("ImageID", imageId);
intent.putExtra("Extra", "2");
appContext.startService(intent);
System.out.println("after service start");
}
});
where ReportImageService.class is the service that I want to start. when i'm pressing the report button nothing happens..
what can be the problem? I can only assume that there is some problem with the applicationContext
回答1:
I had the same problem with my app. The solution that worked for me was: instead of passing your context and use it later (with getContextApplication() method), there is another way to do it, pass:
YourActivityName.this
as your context, and then call your startService() method from this object.
来源:https://stackoverflow.com/questions/15533198/starting-a-service-from-dialog-box-in-android