How to close the activity from the service?

后端 未结 3 1174
旧时难觅i
旧时难觅i 2020-12-05 19:07

I am launching activity from the service based on some value got from the server and the activity will be displayed for the some time and after getting close instruction fro

3条回答
  •  忘掉有多难
    2020-12-05 19:22

    Write a broadcast receiver in your CustomDialogActivity like following.

    private final BroadcastReceiver abcd = new BroadcastReceiver() {
                 @Override
                 public void onReceive(Context context, Intent intent) {
                       finish();                                   
                 }
         };
    

    Then register it in the same file like the following:

    onCreate(){
    
        registerReceiver(abcd, new IntentFilter("xyz"));
    }
    

    Unregister it in onDestroy.

    onDestroy(){
    
       //unRegister
    }
    

    Now,Whenever you want to close that Activity just call like the following.

    sendBroadcast(new Intent("xyz"));
    

    Hope this help.

提交回复
热议问题