在后台运行Intent服务

时间:2017-08-23 11:54:18

标签: java android

我正在尝试在后台运行intent服务,即使该应用程序已关闭并已编写此代码。但该服务并不在后台运行。这是我的代码。

MainActivity.java

package com.example.h.intentservice;

公共类MainActivity扩展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void startService(View view){
    Intent intent = new Intent(this,MyIntentService.class);
    startService(intent);

}

public void stopService(View view){
  Intent intent=new Intent(this,MyIntentService.class);
    stopService(intent);
}

}

MyIntentService.java

public class MyIntentService扩展了IntentService {

public MyIntentService() {
    super("My_Worker_Thread");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this,"Service started",Toast.LENGTH_LONG).show();
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this,"Stopped",Toast.LENGTH_LONG).show();
}

@Override
protected void onHandleIntent(Intent intent) {

    synchronized (this){
        int count=0;
        while(count<=10)
        {
            try{
                wait(1500);
                count++;
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

首先,关闭应用程序后,intent服务无法在后台运行。一个intentService只运行一次,一旦分配给它的任务完成,它就会自动关闭该线程。您必须使用服务来处理后台请求。 其次,您没有从onCreate调用startService()方法,因此尚未启动serviceIntent。 在startService()方法和stopService()方法中,您传递的视图在方法体内部未被使用。因此,我可以建议您以这种方式调整代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startService();

    //here you have started the service intent
    //if you want to stop the same just call stopService()
}

public void startService(){
    Intent intent = new Intent(this,MyIntentService.class);
    startService(intent);

}

public void stopService(){
  Intent intent=new Intent(this,MyIntentService.class);
    stopService(intent);
}

我还发布了可以在后台运行的服务的示例代码。

以下是服务的示例代码

public class UploadService extends Service {

 public int counter = 0;

    public UploadService(Context applicationContext) {
        super();

        Log.i("SERVICE", "hService started");
    }

    public UploadService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        //instantiate your variables here

        //i call my startUpload method here to doing the task assigned to it

        startUpload();

        return START_STICKY;
    }



    public void startUpload() {

    //call the method with your logic here

    //mine is a sample to print a log after every x seconds

        initializeTimerTask();

    }

    /**
     * it sets the timer to print the counter every x seconds
     */
    public void initializeTimerTask() {
        // timerTask = new TimerTask() {

        //we can print it on the logs as below
        Log.i("in timer", "in timer ++++  " + (counter++));

        //or use the print statement as below
        System.out.println("Timer print " + counter++);

    }

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


      @Override
    public void onDestroy() {
        super.onDestroy();
        stopSelf();
    }

}
相关问题