2012-07-20 154 views
5

我構建了自定義鎖定屏幕應用程序,該應用程序使用廣播接收器和服務來偵聽用戶何時打開或關閉屏幕,並從那裏啓動我的活動。該活動應該完全取代鎖定屏幕。爲了做到這一點,我的應用程序應該禁用Android股票鎖,以便我的應用程序可以作爲新的鎖定屏幕。自定義鎖定屏幕應用程序中的android鎖定問題

取而代之的是,一旦應用程序第一次安裝,服務首次啓動應用程序似乎正在工作。當用戶首次關閉手機的屏幕時,他們將會看到我的應用程序在頂部運行,並且能夠使用我的應用程序解鎖他們的手機。但是如果用戶按下home按鈕,那麼一旦進入android操作系統,他們下一次關閉屏幕並將其重新打開,而不是被帶回到我的應用程序,它們被帶到股票解鎖屏幕,我的應用程序在其下面打開,當它應該是最高的。

這裏是我的代碼:

我的服務:

public class MyService extends Service { 
    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() { 

     super.onCreate(); 
     Log.d("MyService","Service STARTED"); 
     final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
     filter.addAction(Intent.ACTION_SCREEN_OFF); 
     final BroadcastReceiver mReceiver = new ScreenReceiver(); 
     registerReceiver(mReceiver, filter); 
    } 
} 

我的廣播接收器:

public class ScreenReceiver extends BroadcastReceiver { 

public static ArrayList<String> runningApplications = new ArrayList<String>(); 
private Context ctext; 
public static boolean screenIsLocked; 
public static KeyguardManager keyguardManager; 
public static KeyguardLock lock; 

@Override 
public void onReceive(final Context context, final Intent intent) { 
    ctext = context; 
    keyguardManager = (KeyguardManager)ctext.getSystemService(Activity.KEYGUARD_SERVICE); 
    lock = keyguardManager.newKeyguardLock(Context.KEYGUARD_SERVICE); 
    lock.disableKeyguard(); 


    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
     screenIsLocked = true; 
     Log.d("ScreenReceiver", "False"); 

     Intent intenti = new Intent(); 
     intenti.setClass(context, starterActivity.class); 
     intenti.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intenti.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(intenti); 


    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
     screenIsLocked = false; 
     Log.d("ScreenReceiver", "True"); 

        Intent intenti = new Intent(); 
        intenti.setClass(context, starterActivity.class); 
        intenti.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        intenti.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        context.startActivity(intenti); 
    } 
} 

我所啓動的活動是隻用一個解鎖按鈕調用finish();基本清空當按下時。

+0

這可能是因爲該應用程序被設置....不此問題發生連續?? – jaisonDavis 2012-07-21 15:27:19

回答

2

我試圖編譯你的代碼,並得到了你說的同樣的錯誤。我試圖修改它,使其工作,並最終得到了問題!

public class ScreenReceiver extends BroadcastReceiver { 

    public static ArrayList<String> runningApplications = new ArrayList<String>(); 
    private Context ctext; 
    public static boolean screenIsLocked; 
    public static KeyguardManager keyguardManager; 
    public static KeyguardLock lock; 

    @Override 
    public void onReceive(final Context context, final Intent intent) { 
     ctext = context; 
     keyguardManager = (KeyguardManager)ctext.getSystemService(Activity.KEYGUARD_SERVICE); 
     lock = keyguardManager.newKeyguardLock(Context.KEYGUARD_SERVICE); 
     lock.disableKeyguard(); 


     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
      screenIsLocked = true; 
      Log.d("ScreenReceiver", "False"); 

      Intent intenti = new Intent(); 
      intenti.setClass(context, starterActivity.class); 
      intenti.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      intenti.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(intenti); 


     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
      screenIsLocked = false; 
      Log.d("ScreenReceiver", "True"); 

         Intent intenti = new Intent(); 
         intenti.setClass(context, starterActivity.class); 
         intenti.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         intenti.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         context.startActivity(intenti); 
     } 
    } 

隨着這一變化給廣播接收機類,我能夠克服的問題

試試吧,告訴我,如果有任何問題。

編輯:我認爲這個問題可能出在finish()方法.... Android轉儲應用程序,當它需要內存...我認爲finish()可能是幫助Android搗毀應用程序(這可能是你的原因問題隨機發生)

+1

不是從我的應用程序中,而不是從android操作系統中唯一的工作? – Peter 2012-07-21 22:46:35

+0

對不起,我不明白你說「從Android OS」 – jaisonDavis 2012-07-22 03:24:57

+0

我剛剛試了一下,現在它不會立即返回到鎖屏,但下一次我解鎖設備不會去我的應用程序是什麼精神疾病調出默認解鎖器 – Peter 2012-07-23 15:53:51

4

鎖定鍵相關邏輯的行爲因設備而異。這是因爲鎖屏通常是由設備製造商定製的(即沒有庫存),其中一些尊重您使用的鍵盤鎖邏輯,有些則不。

而且,據我所知,控制鍵盤鎖的新方法是使用窗口標誌:

// inside activity 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
     | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 

這雖然不會解決問題,設備仍然有他們關於這個發言權。

E.g.根據我的經驗,Galaxy Nexus會在keyguard上方顯示您的活動窗口,但不會忽略它(您認爲Google品牌的設備應該尊重該標誌,呃),所以如果您在活動中點擊後退按鈕 - 您將獲得標準鎖屏 - 而HTC One X似乎正確處理解散部分:您的活動窗口將導致標準鎖屏按預期被解僱。

我發現沒有辦法強制所有設備正常工作。 Android API無意讓您創建自定義鎖定屏幕(至少目前不是)。看看商店裏的人 - 他們都有不完全穩定的問題。

由於戴安娜Hackborn says in this Google Groups answer,任何你能在這方面做的是一個黑客這樣期待它不時打破時間。