小编Law*_*hoy的帖子

在onDestroy()触发后,IntentService onHandleIntent()仍在运行

在我的首选项屏幕中,我想启动一项服务,以便在点击其中一个首选项时从互联网上下载文件.如果服务已在运行(下载文件),则应停止服务(取消下载).

public class Setting extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    downloadPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference pref) {
            if (DownloadService.isRunning) {
                Setting.this.stopService(new Intent(Setting.this,
                    DownloadService.class));
            } else {
                Setting.this.startService(new Intent(Setting.this,
                    DownloadService.class));
            }
            return false;
        }
    });
    }
}
Run Code Online (Sandbox Code Playgroud)

服务类:

public class DownloadService extends IntentService {

public static final int DOWNLOAD_SUCCESS = 0;
public static final int DOWNLOAD_FAIL = 1;
public static final int DOWNLOAD_CANCELLED = 2;
public static final int SERVER_FAIL = 3;

public static boolean …
Run Code Online (Sandbox Code Playgroud)

android android-service intentservice

3
推荐指数
1
解决办法
4247
查看次数

标签 统计

android ×1

android-service ×1

intentservice ×1