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.
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With