2017-02-12 147 views
2

我看到有多個關於SO的問題,表示當另一個應用程序想要使用麥克風時(Unable to access microphone when another app is using it in Android),無法看到它。但是,我知道這是可能的,因爲Shazam具有「自動」功能,可以實現我想要的確切功能:在其他應用程序不使用麥克風時不斷地收聽音頻。檢測是否有其他應用程序正在請求麥克風

目前,當我使用我的應用程序並使用像Snapchat這樣的應用程序時,我無法錄製帶有音頻的視頻,因爲Snapchat不會帶麥克風。但是,正如我之前所說的,在這種情況下,Shazam的Auto功能正常工作。

那麼,如何聽其他應用程序想要使用麥克風?我很好,只要它不需要生根電話或類似的東西,就可以使用「黑客」。

編輯: Shazam從來沒有這種功能,他們的應用程序無法在運行時將麥克風放棄到其他應用程序。

+0

你有一個解決方案,我奮力爲 –

+1

@KareemElsayed我貼我的最佳解決方案 – HaydenKai

回答

1

這是我目前最好的解決方案。沒有辦法監聽請求麥克風的其他應用程序,因此我創建了一個每3秒運行一次的UsageStats檢查器,以查看當前打開的應用程序是否有能力請求音頻。請讓我知道,如果你有更好的東西或改善這一點。

注:必須將權限添加到應用程序清單:

<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" />

AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE); 
    int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, 
      android.os.Process.myUid(), getPackageName()); 

    if (mode != AppOpsManager.MODE_ALLOWED) { 
     Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); 
     startActivity(intent); 
    } 

    if (otherAppAudioTimer != null) { 
     otherAppAudioTimer.cancel(); 
    } 

    otherAppAudioTimer = new Timer(); 
    otherAppAudioTimer.scheduleAtFixedRate(new TimerTask() { 
     @Override 
     public void run() { 
      UsageStatsManager usm = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE); 
      long now = System.currentTimeMillis(); 
      final List<UsageStats> queryUsageStats = usm.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY, now - (1000 * 60 * 60), now); 
      if (queryUsageStats != null && queryUsageStats.size() > 0) { 
       SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>(); 
       for (UsageStats usageStats : queryUsageStats) { 
        mySortedMap.put(usageStats.getLastTimeUsed(), usageStats); 
       } 
       if (!mySortedMap.isEmpty()) { 
        String currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName(); 
        boolean hasRecordAudio = getPackageManager() 
          .checkPermission(Manifest.permission.RECORD_AUDIO, currentApp) 
          == PackageManager.PERMISSION_GRANTED; 

        if (getApplicationContext().getPackageName().equals(currentApp)) { 
         Log.e("hasAudio", "Current app is self"); 
         return; 
        } 


        if (hasRecordAudio) { 
         //the current app can record audio 
        } else { 
         //the current app cannot record audio 
        } 

       } 
      } 
     } 
    }, 0, 3000); 
+0

從[文檔](https://developer.android.com/reference/android/app/usage/UsageStatsManager.html):'PACKAGE_USAGE_STATS'。 。 。是系統級權限,不會授予第三方應用程序。但是。 。 。設備的用戶可以通過設置應用程序授予權限。 –

+0

@KevinKrumwiede是的,它不是由Android隱式授予的,但是在我的代碼中,Intent代碼發出請求以允許用戶尚未擁有的權限。這也是爲什麼我需要在權限中使用忽略 – HaydenKai

+0

['ACTION_USAGE_ACCESS_SETTINGS'](https://developer.android.com/reference/android/provider/Settings.html#ACTION_USAGE_ACCESS_SETTINGS)的文檔說它可能不會適用於所有設備。在棒棒糖的早期,我讀到包括三星和LG在內的主要製造商已將其從許多設備中移除。你遇到過這個問題嗎? –

相關問題