How to close the activity from the service?

后端 未结 3 1157
旧时难觅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.

    0 讨论(0)
  • 2020-12-05 19:34

    Try to send broadcast using context.

    context.sendBroadcast(new Intent("xyz"));
    

    Answer from Jainal is Working for me.

    0 讨论(0)
  • 2020-12-05 19:39

    You must add the receiver into the manifest for your activity, For example :

        <activity  android:name="MyActivity">
            <intent-filter>
                <action android:name="xyz" />
            </intent-filter>
        </activity>
    
    0 讨论(0)
提交回复
热议问题