2011-08-04 43 views
0

我創建,通過短信trigerred我的鎖屏的應用程序..我有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-鎖定「命令時,它將進入lockscreen.java,當它接收到命令」andro-unclock「時,會通過額外的(putExtra)」kill「進入lockscreen.java。這裏是我的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(); 
      } 

     } 

    } 

我想在我的ListenSMS服務收到「andro-unlock」命令時關閉我的locksreen.java,因此我在意圖上添加了額外的內容(「kill」)並檢查它在lockscreen.java ..這lockscreen.java可以檢查額外的意圖,並可以顯示一個吐司「額外接受」,但可以關閉lockscreen活動與完成()..

我的假設是現在的意圖.FLAG_ACTIVITY_NEW_TASK正在複製一個locksreen活動..所以它會創建一個雙重鎖屏活動,並且finish方法正在關閉由Intent.FLAG_ACTIVITY_NEW_TASK啓動的另一個lockscreen.java ..這只是假設..我錯了嗎?請糾正我..

有誰知道如何解決我的問題??我真的希望「雄激素 - 解鎖」命令可以關閉鎖屏活動,並需要它爲我的大學最後的項目..請幫助..

問候,

邁克爾..

回答

1

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,然後當收到解鎖消息時發送一個Intent,以表示BroadcastReceiver將捕獲。該活動然後可以乾淨地退出。

+0

對不起,我不明白你的建議,因爲我仍然是Android新手..你知道如何關閉服務的活動?謝謝 –

+1

要關閉一個活動,你需要給它發送某種信號。最簡單的信號是一個意圖。您可以按照http://stackoverflow.com/questions/4794776/start-broadcastreceiver-from-activity中所述註冊BroadcastReceiver,然後只是激發一個匹配的Intent。 – Femi

+0

我在listenSMS服務做了一個意圖信號..我被放在額外的意圖(「殺死」),並在lockscreen.java讀取它們。lockscreen.java已收到信號,它可以顯示一個「額外接受」的敬酒,但無法運行完成()方法?你知道爲什麼嗎??這裏真的很困惑..謝謝.. –