gpt4 book ai didi

android - 从android中的对话框启动服务

转载 作者:行者123 更新时间:2023-11-29 21:46:56 26 4
gpt4 key购买 nike

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

这是打开对话框的代码:

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 有问题

最佳答案

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

YourActivityName.this

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

关于android - 从android中的对话框启动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15533198/

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