Android 即使应用程序已关闭,如何继续运行任务?

Android 即使应用程序已关闭,如何继续运行任务?,android,Android,目前,我已经构建了一个服务,每15秒对我的php服务器执行一次httpRequest。代码运行良好,但问题是当我关闭应用程序时,httpRequest停止工作,不再发送数据。我想做的是当我关闭应用程序时,httpRequest仍在运行。我应该在代码中更改什么 这是我一直在做的代码 public class NotificationService extends Service { public String response; @Override public IBinder onBind(

目前,我已经构建了一个服务,每15秒对我的php服务器执行一次httpRequest。代码运行良好,但问题是当我关闭应用程序时,httpRequest停止工作,不再发送数据。我想做的是当我关闭应用程序时,httpRequest仍在运行。我应该在代码中更改什么

这是我一直在做的代码

public class NotificationService extends Service {

public String response;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "on start command", Toast.LENGTH_SHORT).show();
    new MyAsyncTask().execute("muhammad sappe");
    return START_NOT_STICKY;
}

private class MyAsyncTask extends AsyncTask<String, String, String>{

  @Override
  protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    postData(params[0]);
    return null;
  }

  @Override
  protected void onPostExecute(String result) {
      new Handler().postDelayed(new Runnable() {
            public void run() {
                new MyAsyncTask().execute("muhammad sappe");
            }
        }, 15000);
    super.onPostExecute(result);
  }

}

public void postData(String MyName){
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://mydomain/getData.php");
    try {   
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("action", MyName));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        // Execute HTTP Post Request
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        response = httpclient.execute(httppost, responseHandler);
    }catch(ClientProtocolException e){
        // TODO Auto-generated catch block
    }catch(IOException e) {
        // TODO Auto-generated catch block
    }
}

public void onDestroy() {
    super.onDestroy();
}
}
公共类NotificationService扩展服务{
公共字符串响应;
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
Toast.makeText(这是“启动命令时”,Toast.LENGTH_SHORT.show();
新建MyAsyncTask().execute(“muhammad sappe”);
返回开始时间不粘;
}
私有类MyAsyncTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
postData(参数[0]);
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
new Handler().postDelayed(new Runnable()){
公开募捐{
新建MyAsyncTask().execute(“muhammad sappe”);
}
}, 15000);
super.onPostExecute(结果);
}
}
公共void postData(字符串MyName){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://mydomain/getData.php");
试试{
//添加您的数据
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“action”,MyName));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
ResponseHandler ResponseHandler=新BasicResponseHandler();
response=httpclient.execute(httppost,responseHandler);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
公共空间{
super.ondestory();
}
}

您必须使用前台服务

前台服务必须具有通知,以通知用户即使应用程序已关闭,仍有服务正在运行

以下是一个例子:

public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    if(intent!=null) {
        if(intent.hasExtra("id")) {
            url = intent.getStringExtra("id");
            int id = 1;
            // Issues the notification
            n = new NotificationCompat.Builder(this).setContentTitle("Workee")
                    .setContentText("Deleting Image")
                    .setProgress(0, 0, true)
                    .setAutoCancel(false)
                    .setSmallIcon(R.drawable.workee_logo).build();
            startForeground(id, n);
            // start thr asnyc task
            deletePhoto();
            return START_STICKY;
        }
    }

你必须使用前台服务

前台服务必须具有通知,以通知用户即使应用程序已关闭,仍有服务正在运行

以下是一个例子:

public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    if(intent!=null) {
        if(intent.hasExtra("id")) {
            url = intent.getStringExtra("id");
            int id = 1;
            // Issues the notification
            n = new NotificationCompat.Builder(this).setContentTitle("Workee")
                    .setContentText("Deleting Image")
                    .setProgress(0, 0, true)
                    .setAutoCancel(false)
                    .setSmallIcon(R.drawable.workee_logo).build();
            startForeground(id, n);
            // start thr asnyc task
            deletePhoto();
            return START_STICKY;
        }
    }
如前所述,您需要使用
前台服务
。那是错误的!您不需要使用
前台服务
。你需要做的就是改变你的返回值

发件人:

return START_NOT_STICKY;
致:

前台服务
用于当您需要显示应用程序正在后台执行某些任务的通知时。例如音乐播放器应用程序。

如前所述,您需要使用
前台服务。那是错误的!您不需要使用
前台服务
。你需要做的就是改变你的返回值

发件人:

return START_NOT_STICKY;
致:


前台服务
用于当您需要显示应用程序正在后台执行某些任务的通知时。例如音乐播放器应用程序。

这不是我要找的,但感谢你指出这一点,这让我想到这应该用在我的应用程序中:)这不是我要找的,但感谢你指出这一点,这让我想到这应该用在我的应用程序中:)我的错误:D我不理解START\u粘性和开始不粘。。。。。。非常感谢兄弟(y)…你节省了我的时间我的错误:D我错过了关于开始粘性和开始粘性的理解。。。。。。非常感谢兄弟…你节省了我的时间