简体   繁体   English

如何关闭服务中的活动(finish())?

[英]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 : 我制作了一个锁屏应用程序。.我有一些监听SMS的服务..当SMS接收命令andro-lock时,我会显示一个由服务调用的锁屏:这是该服务的代码:

 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?? 该代码可以正常运行,但是当我尝试解锁手机时我遇到了一些问题..如果我的服务收到命令andro-unlock,那么我必须关闭(完成)那个lockscreen.java ..我正在尝试许多代码,但仍然无法正常工作..当我的服务收到命令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 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. 您可以向活动发送意图,活动可以读取此活动,如果有的话,可以读取finish()本身。

Regards, Stéphane 问候,斯特凡

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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