1

我正在尝试从弹出对话框启动后台服务,但它对我不起作用

这是打开对话框的代码:

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

在 ReportWrongLangDialog 我保存 appContext 和 imageId

在按下报告按钮时的对话框中,我想启动将报告图像的后台服务

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

其中 ReportImageService.class 是我要启动的服务。当我按下报告按钮时没有任何反应..

可能是什么问题?我只能假设 applicationContext 有问题

4

1 回答 1

2

我的应用程序遇到了同样的问题。对我有用的解决方案是:与其传递您的上下文并稍后使用它(使用 getContextApplication() 方法),还有另一种方法可以做到这一点,通过:

YourActivityName.this

作为您的上下文,然后从此对象调用您的 startService() 方法。

于 2013-03-22T21:31:53.507 回答