Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

android - capture permission granted complete event

I'm creating an app using flash cc. I needed storage permission. It turns out I needed to ask user the permission for using storage devices. I can ask user for permission and it is working fine. I use examples from this website: https://helpx.adobe.com/flash-player/release-note/fp_24_air_24_release_notes.html
But my problem is I wasn't able to capture the complete event for accessing the storage permission. Because of that I couldn't run codes after I get access to storage. Is it possible to capture complete event for granting any permission?

the code I used:

var file:File = File.documentsDirectory.resolvePath("somefile.txt");
trace("url_txt:" + file.url);
file.addEventListener(PermissionEvent.PERMISSION_STATUS, function permissionStatusHandler(e:PermissionEvent):void
{                        
    file.removeEventListener(PermissionEvent.PERMISSION_STATUS, permissionStatusHandler);
    if(e.status == PermissionStatus.GRANTED)
    {                            
        myTextLoader.load(new URLRequest(file.url));
        myTextLoader.addEventListener(Event.COMPLETE, onLoadTextComp);
        myTextLoader.addEventListener(IOErrorEvent.IO_ERROR, loadingTextError);
    }
    else
    {
        showPermissionError();
    }
}
);
try
{
    trace("Requesting permission");
    file.requestPermission();
}
catch(error:Error)
{
    trace("Request permission error");
}

UPDATE:

The above code seems to work fine. But the problem occurs when I tried to request for same permission twice at different time. I've another question. Can we add description while we request permission? A lot of app seems to be adding description why the app need that particular permission. Is it possible do achieve this from flash as3? I've looked into web but couldn't find anything. And how to request permission for READ_PHONE_STATE?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Finally, I figured it out. Prerequisites:

  • AIR runtime 24+
  • Android 6+
  • APK must be published with WRITE_EXTERNAL_STORAGE permission (otherwise it is automatically DENIED - that what I was stuck at)

Then, this code works for me just fine, it displays Android's "Grant Permission" dialog and then outputs GRANTED or DENIED with regard to my choice. The Log class is just a debug panel of my own, you can change Log.log calls to trace or grab it here (it has no dependencies): https://bitbucket.org/thydmitry/ru.delimiter/src/9083fb46ce1c/classes/ru/delimiter/utils/

package
{
    import ru.delimiter.utils.Log;

    import flash.filesystem.File;

    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Sprite;

    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.PermissionEvent;

    import flash.permissions.PermissionStatus;

    public class Permissions extends Sprite
    {
        private var F:File;

        public function Permissions() 
        {
            if (stage) onStage();
            else addEventListener(Event.ADDED_TO_STAGE, onStage);
        }

        private function onStage(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, onStage);

            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;

            Log.create(this, true);

            Log.log("[Permissions Test] started");
            Log.log("File.permissionStatus:", File.permissionStatus);

            F = File.applicationStorageDirectory.resolvePath("somefile.txt");
            F.addEventListener(PermissionEvent.PERMISSION_STATUS, onPerm);

            stage.addEventListener(MouseEvent.CLICK, onClick);
        }

        private function onClick(e:MouseEvent):void
        {
            F.requestPermission();
        }

        private function onPerm(e:PermissionEvent):void
        {
            Log.log("User's decision:", e.status.toUpperCase());
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...