How to stop a service in a block method in Android?

  • I run the code and get the following result, but I hope the application can work in the order "A" → "OnDestroy Service" → "B" → "C", how can I do it?

  • In My path 2 section, I try to put the code in a function new Handler().postDelayed(new Runnable() {}, this is normal, it worked in the order "A" → "OnDestroy Service" → "B" → "C", I don’t know why the path can be successful, I don’t know is this a good way!

Result

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: Service OnDestroy

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");
            }
        });

    }

}

RecordService.cs

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);
    }


}

======================== My path 1 ========================= ================

I set the isServiceStoped label to track the Stop Service completion, but my application freezes after requesting the result "11-13 11: 31: 23.107 7599-7599 / info.dodata.screenrecorder E / My: A"

New 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);

                while (RecordService.isServiceStoped==false){
                    //It block 
                }

                Log.e("My", "B");

                Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();

                Log.e("My", "C");
            }
        });

    }

}

New RecordService.cs

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);
    }


}

====================== My path 2 =========================== ==================

I try to put the code in a function new Handler().postDelayed(new Runnable() {}, this is normal, it worked in the order "A" → "OnDestroy Service" → "B" → "C",

I don’t know why the path can be successful, I don’t know if the way is good.

Latest 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);

                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);


            }
        });

    }

}

Latest RecordService.cs

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);
    }
}
+4
3
  • , . stopService() - . - , .

  • , super.onDestroy() onDestroy() - .

+1
  • , . stopService(), - . , , B, , . .
  • , super.onDestroy() onDestroy, . , . onDestroy() SDK Android:

    @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);
    }
    

* * , .

public class UIAbout extends Activity {
private Handler mHandler = new Handler();
private Runnable checkServiceHandler;
    @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);
                checkServiceHandler = new Runnable() {
               public void run() {
                  if(RecordService.isServiceStoped){
                       mHandler.removeCallbacks(checkServiceHandler );
                       somemethod();
                  } else{
                        mHandler.postDelayed(checkServiceHandler, 500);
                   }
                 }
               };
              mHandler.postDelayed(checkServiceHandler, 500);            }
        });
    }
  private void somemethod(){
                Log.e("My", "B");
                Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
                Log.e("My", "C");  
  }
}
+1

, , .

. , 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

+1

Source: https://habr.com/ru/post/1615679/


All Articles