检测是否有其他应用正在请求麦克风 [英] Detect if another app is requesting the microphone

查看:576
本文介绍了检测是否有其他应用正在请求麦克风的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO上看到了多个问题,说不可能看到其他应用何时要使用麦克风(

I have seen multiple questions on SO that say it is impossible to see when another app wants to use the microphone (Unable to access microphone when another app is using it in Android). However, I know it is possible because Shazam has an "Auto" function that allows the exact functionality I want: to continuously listen to audio when other apps are not using the microphone.

当前,当我使用自己的应用程序并使用诸如Snapchat之类的应用程序时,由于Snapchat无法接管麦克风,因此无法录制带音频的视频.但是,正如我之前所说,在这种情况下,Shazam的自动"功能可以正常工作.

Currently, when I use my app and use an app like Snapchat, I am unable to record a video with audio because Snapchat does not take the mic over. However, as I said before, in this case Shazam's Auto feature works fine.

那么当其他应用程序想要使用麦克风时,我该如何收听?我可以使用黑客"之类的东西,只要它不需要扎根手机或类似物即可.

So how do I listen for when other apps want to use the mic? I am fine using something that is a "hack" as long as it does not require rooting the phone or similar.

Shazam从未拥有此功能,他们的应用程序无法在运行时将麦克风放弃给其他应用程序.

Shazam never had this functionality, their app fails to relinquish the mic to other applications while it is running.

推荐答案

这是我目前拥有的最佳解决方案.无法监听其他要求麦克风的应用程序,因此我创建了一个UsageStats检查器,该检查器每3秒运行一次,以查看当前打开的应用程序是否具有请求音频的功能.请让我知道您是否有更好的地方或对此进行改进.

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" />

<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);

这篇关于检测是否有其他应用正在请求麦克风的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆