如何在Android中用block方法停止服务?

如何在Android中用block方法停止服务?,android,Android,我运行代码并得到以下结果,但我希望应用程序能够以“A”->“Service OnDestroy”->“B”->“C”的顺序运行,我该怎么办 在我的方式2部分,我尝试将代码放入函数new Handler().postDelayed(new Runnable(){},没关系,它以“A”->“Service onestory”->“B”->“C”的顺序运行, 我不知道为什么这条路能成功,我不知道这条路是不是好路 结果 11-13 10:04:32.137 27947-27947/info.dodata

我运行代码并得到以下结果,但我希望应用程序能够以“A”->“Service OnDestroy”->“B”->“C”的顺序运行,我该怎么办

  • 我的方式2部分,我尝试将代码放入函数
    new Handler().postDelayed(new Runnable(){}
    ,没关系,它以“A”->“Service onestory”->“B”->“C”的顺序运行, 我不知道为什么这条路能成功,我不知道这条路是不是好路

  • 结果

    11-13 10:04:32.137 27947-27947/info.dodata.screenrecorder E/My﹕ A

    11-13 10:04:32.147 27947-27947/info.dodata.screenrecorder E/My﹕ B

    11-13 10:04:32.157 27947-27947/info.dodata.screenrecorder E/My﹕ C

    11-13 10:04:32.157 27947-27947/info.dodata.screenrecorder E/My﹕ 服务预订

    UIAbou.cs

    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
                    stopService(intent1);
    
                    Log.e("My", "B");
    
                    Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                    Log.e("My", "C");
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
        private Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My","Service OnDestroy");
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
             return super.onStartCommand(intent,flags,startId);
        }
    
    
    }
    
    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
                    stopService(intent1);
    
                    while (RecordService.isServiceStoped==false){
                        //It block 
                    }
    
                    Log.e("My", "B");
    
                    Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                    Log.e("My", "C");
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
       public static boolean isServiceStoped=true;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My", "Service OnDestroy");
            isServiceStoped=true;
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            isServiceStoped=false;
            return super.onStartCommand(intent,flags,startId);
        }
    
    
    }
    
    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
                    stopService(intent1);
    
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            Log.e("My", "B");
    
                            Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                            Log.e("My", "C");
                        }
                    }, 1);
    
    
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
        private Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My", "Service OnDestroy");
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent,flags,startId);
        }
    }
    
    RecordService.cs

    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
                    stopService(intent1);
    
                    Log.e("My", "B");
    
                    Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                    Log.e("My", "C");
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
        private Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My","Service OnDestroy");
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
             return super.onStartCommand(intent,flags,startId);
        }
    
    
    }
    
    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
                    stopService(intent1);
    
                    while (RecordService.isServiceStoped==false){
                        //It block 
                    }
    
                    Log.e("My", "B");
    
                    Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                    Log.e("My", "C");
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
       public static boolean isServiceStoped=true;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My", "Service OnDestroy");
            isServiceStoped=true;
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            isServiceStoped=false;
            return super.onStartCommand(intent,flags,startId);
        }
    
    
    }
    
    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
                    stopService(intent1);
    
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            Log.e("My", "B");
    
                            Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                            Log.e("My", "C");
                        }
                    }, 1);
    
    
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
        private Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My", "Service OnDestroy");
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent,flags,startId);
        }
    }
    
    =================================================================================================

    我设置了一个标记isServiceStoped来监控停止服务是否完成,但我的应用程序在显示结果“11-13 11:31:23.107 7599-7599/info.dodata.screenrecorder E/my”后挂起﹕ A“

    新UIAbout.cs

    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
                    stopService(intent1);
    
                    Log.e("My", "B");
    
                    Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                    Log.e("My", "C");
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
        private Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My","Service OnDestroy");
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
             return super.onStartCommand(intent,flags,startId);
        }
    
    
    }
    
    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
                    stopService(intent1);
    
                    while (RecordService.isServiceStoped==false){
                        //It block 
                    }
    
                    Log.e("My", "B");
    
                    Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                    Log.e("My", "C");
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
       public static boolean isServiceStoped=true;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My", "Service OnDestroy");
            isServiceStoped=true;
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            isServiceStoped=false;
            return super.onStartCommand(intent,flags,startId);
        }
    
    
    }
    
    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
                    stopService(intent1);
    
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            Log.e("My", "B");
    
                            Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                            Log.e("My", "C");
                        }
                    }, 1);
    
    
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
        private Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My", "Service OnDestroy");
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent,flags,startId);
        }
    }
    
    新的RecordService.cs

    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
                    stopService(intent1);
    
                    Log.e("My", "B");
    
                    Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                    Log.e("My", "C");
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
        private Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My","Service OnDestroy");
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
             return super.onStartCommand(intent,flags,startId);
        }
    
    
    }
    
    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
                    stopService(intent1);
    
                    while (RecordService.isServiceStoped==false){
                        //It block 
                    }
    
                    Log.e("My", "B");
    
                    Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                    Log.e("My", "C");
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
       public static boolean isServiceStoped=true;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My", "Service OnDestroy");
            isServiceStoped=true;
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            isServiceStoped=false;
            return super.onStartCommand(intent,flags,startId);
        }
    
    
    }
    
    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
                    stopService(intent1);
    
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            Log.e("My", "B");
    
                            Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                            Log.e("My", "C");
                        }
                    }, 1);
    
    
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
        private Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My", "Service OnDestroy");
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent,flags,startId);
        }
    }
    
    ===================================================================================================================

    我尝试将代码放入函数
    new Handler().postDelayed(new Runnable(){}
    ,没关系,它以“A”->“Service OnDestroy”->“B”->“C”的顺序运行

    我不知道为什么这条路能成功,我不知道这条路是否好

    最后一个UIAbout.cs

    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
                    stopService(intent1);
    
                    Log.e("My", "B");
    
                    Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                    Log.e("My", "C");
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
        private Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My","Service OnDestroy");
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
             return super.onStartCommand(intent,flags,startId);
        }
    
    
    }
    
    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
                    stopService(intent1);
    
                    while (RecordService.isServiceStoped==false){
                        //It block 
                    }
    
                    Log.e("My", "B");
    
                    Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                    Log.e("My", "C");
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
       public static boolean isServiceStoped=true;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My", "Service OnDestroy");
            isServiceStoped=true;
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            isServiceStoped=false;
            return super.onStartCommand(intent,flags,startId);
        }
    
    
    }
    
    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
                    stopService(intent1);
    
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            Log.e("My", "B");
    
                            Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                            Log.e("My", "C");
                        }
                    }, 1);
    
    
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
        private Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My", "Service OnDestroy");
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent,flags,startId);
        }
    }
    
    最后一个RecordService.cs

    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
                    stopService(intent1);
    
                    Log.e("My", "B");
    
                    Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                    Log.e("My", "C");
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
        private Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My","Service OnDestroy");
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
             return super.onStartCommand(intent,flags,startId);
        }
    
    
    }
    
    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
                    stopService(intent1);
    
                    while (RecordService.isServiceStoped==false){
                        //It block 
                    }
    
                    Log.e("My", "B");
    
                    Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                    Log.e("My", "C");
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
       public static boolean isServiceStoped=true;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My", "Service OnDestroy");
            isServiceStoped=true;
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            isServiceStoped=false;
            return super.onStartCommand(intent,flags,startId);
        }
    
    
    }
    
    public class UIAbout extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_about);
    
            Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
            startService(intent1);
    
            Button btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("My", "A");
    
                    Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
                    stopService(intent1);
    
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            Log.e("My", "B");
    
                            Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
    
                            Log.e("My", "C");
                        }
                    }, 1);
    
    
                }
            });
    
        }
    
    }
    
    public class RecordService extends Service {
    
        private Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate(){
    
        }
    
        @Override
        public void onDestroy(){
            Log.e("My", "Service OnDestroy");
            super.onDestroy(); //It seems that the APP is OK if I remove this.
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent,flags,startId);
        }
    }
    
  • 不,您不能同步停止服务。stopService()是停止服务的请求。它将在稍后尽快停止

  • 不,您不能从onDestroy()方法中删除super.onDestroy(),但仍能使其正常工作

  • 您无法控制完全停止正在运行的服务的时间。请使用stopService(),其余的都是您无法控制的。您可以使用处理程序监视服务是否在移动到B之前停止,尽管我不确定您为什么要这样做。这不是一个好的做法
  • 是的,您可以删除onDestroy中的super.onDestroy(),但我不建议您这样做。您的应用程序可能会运行,但会留下不需要的资源。 以下是onDestroy()在android SDK中的外观:

    @CallSuper
    protected void onDestroy() {
        if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
        mCalled = true;
    
        // dismiss any dialogs we are managing.
        if (mManagedDialogs != null) {
            final int numDialogs = mManagedDialogs.size();
            for (int i = 0; i < numDialogs; i++) {
                final ManagedDialog md = mManagedDialogs.valueAt(i);
                if (md.mDialog.isShowing()) {
                    md.mDialog.dismiss();
                }
            }
            mManagedDialogs = null;
        }
    
        // close any cursors we are managing.
        synchronized (mManagedCursors) {
            int numCursors = mManagedCursors.size();
            for (int i = 0; i < numCursors; i++) {
                ManagedCursor c = mManagedCursors.get(i);
                if (c != null) {
                    c.mCursor.close();
                }
            }
            mManagedCursors.clear();
        }
    
        // Close any open search dialog
        if (mSearchManager != null) {
            mSearchManager.stopSearch();
        }
    
        getApplication().dispatchActivityDestroyed(this);
    }
    

    您应该以一种不在乎服务何时被破坏的方式实现代码。

    无论如何,如果你真的需要确切的时间,你可以使用Android的广播系统从你的服务中激发一个意图

    为您服务:

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(CONST_SERVICE_DESTROYED));
    }
    
    在您的活动中:

    private BroadcastReceiver receiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            // your B here
    
            // your C here
        }
    };
    
    您需要像这样注册和注销您的接收者:

    @Override
    protected void onResume()
    {
        super.onResume();
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter(CONST_SERVICE_DESTROYED));
    }
    
    @Override
    protected void onPause()
    {
        super.onPause();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
    }
    

    可以找到Android广播系统的一个很好的解释和示例

    谢谢,事实上,我已经删除了super.onDestroy(),但是应用程序运行得很好!是否有一个标记我可以监控服务是否停止,如果是,我可以这样做。while(isServiceStoped==false){//block}if(isServiceStoped==true){doSomething();}谢谢!请查看我修改过的帖子。我设置了一个标记isServiceStoped来监视停止服务是否完成,但我的应用程序在显示结果“11-13 11:31:23.107 7599-7599/info.dodata.screenrecorder E/my”后挂起﹕ 最后,应用程序崩溃了,显示应用程序没有响应错误!我修改了我的帖子,我尝试将代码放入函数new Handler().postDelayed(new Runnable(){},没关系,它以“A”->“Service onestory”->“B”->“C”的顺序运行,我不知道为什么这种方法会成功,我不知道这种方法是不是好方法,永远不用(RecordService.isServiceStoped==false),它将阻止您的UI。请改用Handler。谢谢!您能用“Handler”给我一个示例代码吗?谢谢!我将尝试您的代码。此外,我尝试将代码“Log.e”(“My”,“B”);…”放入函数new Handler().postDelayed(new Runnable(){},你可以在我的中篇文章中看到,没关系,它的运行顺序是“A”->“Service OnDestroy”->“B”->“C”,我不知道为什么这条路会成功,我不知道这条路是否好谢谢!但我需要关心我的服务何时被破坏,因为我需要处理服务生成的数据。好吧,当你的服务收集到数据后,广播一个意图将对你有效。如果你有任何问题,我会在这里回答:)