Android 如何在执行后停止服务并启动新活动?

Android 如何在执行后停止服务并启动新活动?,android,service,android-intent,Android,Service,Android Intent,我想停止我的服务,直到它完成它的任务。但是服务不会停止,因为我覆盖了onDestroy(),但它不起作用。当服务停止时,它将启动一个新的活动。下面是我的代码 DataService.java 包com.remote.synchronizer.haris; 导入java.util.ArrayList; 导入java.util.List; 导入java.util.Timer; 导入java.util.TimerTask; 导入org.apache.http.NameValuePair; 导入andr

我想停止我的服务,直到它完成它的任务。但是服务不会停止,因为我覆盖了onDestroy(),但它不起作用。当服务停止时,它将启动一个新的活动。
下面是我的代码

DataService.java

包com.remote.synchronizer.haris;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Timer;
导入java.util.TimerTask;
导入org.apache.http.NameValuePair;
导入android.app.Service;
导入android.content.Intent;
导入android.database.sqlite.SQLiteDatabase;
导入android.os.Handler;
导入android.os.IBinder;
导入android.util.Log;
导入android.widget.Toast;
公共类数据服务扩展服务{
布尔wifi,边缘;
专用计时器=新计时器();
sqlitedb数据库;
字符串un、商店、城市、日期、订单;
私有SQLiteAdapter mySQLiteAdapter;
@凌驾
public void onCreate(){
super.onCreate();
mySQLiteAdapter=新的SQLiteAdapter(此);
//这个。stopSelf();
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
最终处理程序mHandler=新处理程序();
新线程(newrunnable()){
@凌驾
公开募捐{
Log.e(“服务启动”、“成功”);
while(true){
试一试{
睡眠(10000);
mHandler.post(新Runnable(){
@凌驾
公开募捐{
//检查网络连接
wifi=NetworkInfo.wifi(DataService.this);
edge=NetworkInfo.edge(DataService.this);
如果(wifi==true | | edge==true)
{
int count=mySQLiteAdapter.getCCount();
int计数器=0;
if(mySQLiteAdapter.getCCount()>0){

while(counter要在工作完成后停止服务,只需调用
stopSelf()
stopSelf(startId)

如果要在服务完成后启动活动,请在调用
stopSelf()
stopSelf(startId)
之前,创建活动的意图并设置标志
minent.addFlags(intent.flag_activity_NEW_TASK);
。 通过调用
startActivity(intent)

  • 如果您需要服务在完成后自行停止,则应使用
  • onDestory()用于释放资源,当服务不再使用时将调用它

  • 开始这样的活动:

    final Handler mHandler = new Handler() {
        public void HandleMessage(Message msg) {
            if(msg.what == START_NEW_ACTIVITY) {
                Intent callIntent = new Intent(Intent.ACTION_CALL); 
                callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                callIntent.setClass(this, com.remote.synchronizer.haris.Login.class);
                startActivity(callIntent);
            }
        }
    };
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread() {
            @Override
            public void run() {
                // do your job here
                mHandler.sendEmptyMessage(START_NEW_ACTIVITY);
           }
        }.start();
    }
    

我认为你应该中断你的线程可以捕捉到中断,除非你的线程继续运行,否则你需要停止(中断)你的线程。
ondestory()
在你的服务中,只有在它停止后才会被安卓调用。@Algo线程用户在
onStartCommand()中启动
在调用
timer.scheduleAtFixedRate()
后完成(即:几乎在启动后立即完成。
计时器
有一个线程,用于将
TimerTask
调度为打开状态。当不再需要时,需要取消此操作。我已更新了上面的类,其中我现在使用了thread.sleep()并尝试中断线程,但它不会停止。当我在onCreate方法中使用此.stopSelf()时,它会破坏服务,但线程仍在后台运行。现在我如何在工作完成后停止线程?
final Handler mHandler = new Handler() {
    public void HandleMessage(Message msg) {
        if(msg.what == START_NEW_ACTIVITY) {
            Intent callIntent = new Intent(Intent.ACTION_CALL); 
            callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            callIntent.setClass(this, com.remote.synchronizer.haris.Login.class);
            startActivity(callIntent);
        }
    }
};
public int onStartCommand(Intent intent, int flags, int startId) {
    new Thread() {
        @Override
        public void run() {
            // do your job here
            mHandler.sendEmptyMessage(START_NEW_ACTIVITY);
       }
    }.start();
}