简体   繁体   中英

How to close an activity (finish()) from service?

i make a lock screen application.. i have some service that listen to the SMS.. when the SMS receiving command andro-lock then i display a lock screen that called by service: here'e the service's code :

 if (tempMessage[0].toString().equalsIgnoreCase("andro-lock") && tempMessage[1].toString().equals(tempPassword.toString())) 
                            {
                                //Toast.makeText(ListenSMSservice.this, "Menjalankan command andro-lock", Toast.LENGTH_LONG).show();
                                openDatabase();
                                updateStatusL();
                                Intent myIntent = new Intent(ListenSMSservice.this,LockScreenForm.class);
                                myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                getApplication().startActivity(myIntent);
                            }

that code has works perfecly, but i have some problem when i try to unlock my phone.. if my service receive command andro-unlock then i must close (finish) that lockscreen.java.. i was trying many code and still not works.. what must i do to close the lockscreen activity when my service receiving command andro-unlock?? please help..

else if (tempMessage[0].toString().equalsIgnoreCase("andro-unlock") && tempMessage[1].toString().equals(tempPassword.toString()))   
                            {
                                //what must i do??
                                //any solution??

                            }

Thanks for your help..

A possible solution to stop the activity would be:

Intent myIntent = new Intent(ListenSMSservice.this,LockScreenForm.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle myKillerBundle = new Bundle();
myKillerBundle.putInt("kill",1);
myIntent.putExtras(myKillerBundle);
getApplication().startActivity(myIntent);

In LockScreenForm

onCreate(Bundle bundle){
if(this.getIntent().getExtras().getInt("kill")==1)
    finish();
}

You could send an intent to your activity with an extra, and the activity could read this extra and, if present, finish() itself.

Regards, Stéphane

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM