Detect if another app is requesting the microphone

坚强是说给别人听的谎言 提交于 2019-12-06 05:35:18

This is currently the best solution I have. There is no way to listen for other applications requesting the microphone, so I created a UsageStats checker that runs every 3 seconds to see if the currently open app has the ability to request audio. Please let me know if you have something better or make improvements to this.

Note: You must add the permissions to the app manifest:

<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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!