Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close this activity?

I created my lockscreen application that trigerred by a SMS.. i have ListenSMS class that always listen for incoming SMS. Here's the code :

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);
    }
}

If ListenSMS service has received an andro-lock command, it will go to the lockscreen.java and will go to the lockscreen.java with intent extra (putExtra) kill when it receive command andro-unclock. Here's my lockscreen.java:

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();
            }

        }

    }
}

I want to close my locksreen.java when my ListenSMS service has received "andro-unlock" command, so I put extra on intent "kill" and check it in lockscreen.java. This lockscreen.java can check the extra intent and can display a toast "extra accepted" but can close the lockscreen activity with finish().

My assumption is for now that Intent.FLAG_ACTIVITY_NEW_TASK is duplicating a locksreen activity. So it will create a double lockscreen activity and the finish method is closing another lockscreen.java that started by Intent.FLAG_ACTIVITY_NEW_TASK. That's only assumption. Am i wrong? Please correct me.

Has anyone know how to solve my problem? I really want that "andro-unlock" command can close the lockscreen activity and need it works for my college final project. Please help.

like image 328
Michael Frans Avatar asked Aug 04 '11 03:08

Michael Frans


People also ask

How do you close an activity?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do you end an activity in Java?

Navigate to app>java>your app's package name>Right click on it>New>Empty Activity>Name it as >Main2Activity and click on Finish to create a new activity.


1 Answers

From 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. 

I expect your problem is somewhere else. I'd suggest having the lockscreen Activity register a BroadcastReceiver, and then when the unlock message is received send an Intent that the BroadcastReceiver will catch. The Activity can then cleanly exit.

like image 101
Femi Avatar answered Sep 28 '22 05:09

Femi