如何关闭此活动?

时间:2011-08-04 03:31:09

标签: java android android-layout android-ndk android-manifest

我创建了一个由短信攻击的锁屏应用程序..我有ListenSMS类,总是收听传入的短信。这是代码:

for (SmsMessage message : messages) {
    String tempMessage[] = message.getDisplayMessageBody().toString().split(" ");

    //checking command dan password                             
    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);
    }
    else if (tempMessage[0].toString().equalsIgnoreCase("andro-unlock") && tempMessage[1].toString().equals(tempPassword.toString())) {
        //Toast.makeText(ListenSMSservice.this, "Menjalankan command andro-unlock", Toast.LENGTH_LONG).show();
        openDatabase();
        updateStatusNL();                                                                   
        Intent myIntent = new Intent(ListenSMSservice.this,LockScreenForm.class);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Bundle myKillerBundle = new Bundle();
        myKillerBundle.putString("kill","1");
        myIntent.putExtras(myKillerBundle);
        getApplication().startActivity(myIntent);
    }
}

如果ListenSMS服务收到andro-lock命令,它将转到lockscreen.java,并在收到命令时将使用intent extra(putExtra)kill转到lockscreen.java {1}}。 这是我的lockscreen.java:

andro-unclock

当我的public class LockScreenForm extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.lockscreen); Bundle extra = getIntent().getExtras(); if (extra == null) { return; } //Toast.makeText(this, extra.getString("kill"), 1).show(); else if(this.getIntent().getExtras().getString("kill").equalsIgnoreCase("1")) { try { Toast.makeText(this, "extra accepted", 1).show(); finish(); } catch (Exception e) { // TODO: handle exception Toast.makeText(this, e.getMessage(), 1).show(); } } } } 服务收到“andro-unlock”命令时,我想关闭我的locksreen.java,所以我把额外的意图“kill”并在lockscreen.java中检查它。这个lockscreen.java可以检查额外的意图,并可以显示“额外接受”的吐司,但可以用finish()关闭锁屏活动。

我的假设是ListenSMS正在重复锁定活动。因此,它将创建一个双锁屏活动,finish方法正在关闭由Intent.FLAG_ACTIVITY_NEW_TASK启动的另一个lockscreen.java。这是唯一的假设。我错了吗?请纠正我。

有谁知道如何解决我的问题?我真的希望“andro-unlock”命令可以关闭锁屏活动,并且需要它适用于我的大学最终项目。请帮忙。

2 个答案:

答案 0 :(得分:4)

来自http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior. 

我希望你的问题出在其他地方。我建议让锁屏活动​​注册一个BroadcastReceiver,然后当收到解锁消息时,发送一个BroadcastReceiver将捕获的Intent。然后,活动可以干净地退出。

答案 1 :(得分:-2)

尝试以下操作:Intent.FLAG_ACTIVITY_NEW_TASK