How to request run-time recording permission on An

2020-02-11 14:32发布

问题:

Since Android 6.0, we need to request permissions during app runtime.

In my app, i'm using local storage, camera, geolocation and audio recording permissions.

So, requesting for local storage, camera and geolocation is ok with that snippet : https://github.com/appcelerator-developer-relations/appc-sample-ti510/blob/master/app/controllers/permissions.js

But i've not found how to request for audio recording permission...I actually use titutorial.audiorecorder module for Android and it crashes when i tried to record since Android 6.0 because permission for recording is not enable.

More informations on my environment :

Operating System

Name                        = Mac OS X
  Version                     = 10.11.3
  Architecture                = 64bit
  # CPUs                      = 4
  Memory                      = 17179869184

Node.js
  Node.js Version             = 0.12.7
  npm Version                 = 2.11.3

Titanium CLI
  CLI Version                 = 5.0.6

Titanium SDK
  SDK Version                 = 5.2.0.GA
  SDK Path                    = /Users/****/#/mobilesdk/osx/5.2.0.GA
  Target Platform             = android

Appcelerator Studio 4.5.0.201602170821

tiapp.xml

<manifest>
    <uses-sdk android:minSdkVersion="15"/>
    <uses-sdk android:targetSdkVersion="23"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT"/>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-feature android:glEsVersion="0x00020000" android:required="true"/>
    <uses-permission android:name="com.mycompany.myapp.permission.MAPS_RECEIVE"/>
...
...

</manifest>

回答1:

You need the ti.permissions module and a recompiled version of TiSDK :

  • Go under your Titanium SDK directory eg: /Users/[YOUR USER]/Library/Application Support/Titanium/mobilesdk/osx/
  • Duplicate the 5.2.0.GA directory and rename it as 5.2.0.WhatYouWant
  • Go under android subdir and replace titanium.jar with this jar : https://github.com/AppWerft/Tierstimmenarchiv/blob/master/modules/android/titanium.jar
  • After that in your project, inside the tiapp.xml replace <sdk-version>5.2.0.GA</sdk-version> with your new named SDK (5.2.0.WhatYouWant) don't forget to insert <uses-permission android:name="android.permission.RECORD_AUDIO"/> inside the tiapp.xml under the <android .... > node

and after that, you can request storage permission with this snippet:

if (!require('ti.permissions').hasPermission('android.permission.RECORD_AUDIO'))
require('ti.permissions').requestPermissions(['android.permission.RECORD_AUDIO'], function(e) {
    if (e.success != 0)
        Ti.API.debug("Permissions OK");
    else
        Ti.API.warn("Permissions denied");
});