2016-12-03 88 views

回答

0

獲取列表,然後檢查是否有鎖屏的應用程序包名稱。

下面是代碼:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE); 

long currentMillis = Calendar.getInstance().getTimeInMillis(); 
Calendar cal = Calendar.getInstance(); 

for (ActivityManager.RunningServiceInfo info : services) { 
    cal.setTimeInMillis(currentMillis-info.activeSince); 
    Log.i("TAG", String.format("Process %s has been running since: %d ms",info.process, info.activeSince)); 
} 

的logcat:

TAG: Process com.android.systemui has been running since: 86526 ms 

這是鎖屏^

TAG: Process com.qualcomm.telephony has been running since: 68521 ms 
TAG: Process com.motorola.ccc has been running since: 57456 ms 
TAG: Process com.google.android.music:main has been running since: 26245 ms 
TAG: Process com.android.phone has been running since: 29421 ms 
TAG: Process com.motorola.ccc has been running since: 52141 ms 
TAG: Process system has been running since: 28602 ms 
TAG: Process com.motorola.actions has been running since: 74371 ms 
TAG: Process com.motorola.ccc has been running since: 59166 ms 
TAG: Process com.motorola.process.slpc has been running since: 25483 ms 
TAG: Process com.android.systemui has been running since: 30142 ms 
TAG: Process com.android.bluetooth has been running since: 22187 ms 
TAG: Process system has been running since: 28603 ms 
TAG: Process com.google.android.gms.persistent has been running since: 31621 ms 
TAG: Process com.android.systemui has been running since: 27361 ms 
TAG: Process com.google.android.gms.persistent has been running since: 99678 ms 
TAG: Process com.motorola.contacts.preloadcontacts has been running since: 45603 ms 
TAG: Process com.google.android.gms.persistent has been running since: 73457 ms 
TAG: Process com.google.android.gms.persistent has been running since: 72908 ms 
TAG: Process com.google.android.gms.persistent has been running since: 37251 
+0

這就是我不知道軟件包名稱的問題 –

+1

@AkashKumar你可以從已經提到的問題中獲得軟件包名稱。 – Gattsu

+1

@AkashKumar檢查[this](http://stackoverflow.com/questions/40952185/dynamically-getting-android-lock-screen-package-name/41076162#comment69397047_40952185) – Gattsu

1

您可以任意確定包名通過分析Android日誌來獲得前景的。例如,如果您打開Goog​​le地圖,則單擊設備的「主頁」按鈕將在日誌中顯示此內容(我通常按照ActivityManager字符串進行過濾)。

START u0 {act=android.intent.action.MAIN cat=[android.intent.category.HOME] 
flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher} 

這表明你的主屏幕Activity的包名是com.android.launcher

然而,當我點擊我的Nexus 4家按鈕,顯示從任何應用程序的鎖屏,它從來沒有顯示正在推出的另一款活動。這讓我覺得這不是我們所理解的典型Activity

如果您查看Android源代碼KeyguardViewMediator.java的源代碼,您會發現一個名爲private void doKeyguardLocked(Bundle options)的方法。我從經驗中知道,將源改爲立即從此方法返回將禁用鎖屏。 KeyguardViewMediator.java的來源顯示它在包com.android.keyguard中,我相信這是您正在尋找的包。

至於動態獲取包名,它對我來說似乎不可能。但是,如果您已經提前知道包名,則不需要動態獲取它。

我希望這會有所幫助。