starting a service from dialog box in android

放肆的年华 提交于 2019-12-12 23:38:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!