Android AccessibilityService ReadFile ENOENT

时间:2014-02-04 15:23:24

标签: android android-service android-permissions

我在开发AccessibilityService时遇到了一些问题。 在我看来,该服务无权阅读该文件。该文件存在,在Activity中执行此操作没有问题。我想要做的就是在发出通知时播放声音。但也使用notification.sound = xy;或RingtoneManager.getRingtone(,)。play无效。我可以通过将FileInputStream用于ENOENT错误来减少问题,因此在我看来,不允许该服务读取非私有存储空间上的文件。 媒体放在

Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.siren);

确实有效。是否有可能通过使用服务从内部存储中读取通知声音? 在Nexus 7 4.4和Razr i 4.12上测试,导致相同的行为

public class MyService extends AccessibilityService implements MediaPlayer.OnPreparedListener{
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {        
        final int eventType = event.getEventType();
        switch (eventType) {
            case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
                try{
                MediaPlayer=new MediaPlayer();
                File f = new File("/system/media/audio/notifications/Upsilon.ogg");
                FileInputStream fileInputStream = new FileInputStream(f);
                mMediaPlayer.setDataSource(fileInputStream.getFD());
                fileInputStream.close();
                mMediaPlayer.setOnPreparedListener(this);
                mMediaPlayer.prepareAsync();
                }
                catch(Exception e){e.printStackTrace();}
                break;
        }
    }

    public void onPrepared(MediaPlayer player) {
        player.start();
    }
...
}

这会导致以下错误:

02-04 16:09:29.623: W/System.err(7163): java.io.FileNotFoundException: /system/media/audio/notifications/Upsilon.ogg': open failed: ENOENT (No such file or directory)
02-04 16:09:29.623: W/System.err(7163):     at libcore.io.IoBridge.open(IoBridge.java:409)
02-04 16:09:29.623: W/System.err(7163):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
02-04 16:09:29.623: W/System.err(7163):     at com.example.examplepack.MyService.parseRawMessages(WhatsAppService.java:145)
02-04 16:09:29.623: W/System.err(7163):     at com.example.examplepack.MyService.onAccessibilityEvent(WhatsAppService.java:108)
02-04 16:09:29.623: W/System.err(7163):     at android.accessibilityservice.AccessibilityService$1.onAccessibilityEvent(AccessibilityService.java:556)
02-04 16:09:29.623: W/System.err(7163):     at android.accessibilityservice.AccessibilityService$IAccessibilityServiceClientWrapper.executeMessage(AccessibilityService.java:644)
02-04 16:09:29.623: W/System.err(7163):     at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40)
02-04 16:09:29.623: W/System.err(7163):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-04 16:09:29.623: W/System.err(7163):     at android.os.Looper.loop(Looper.java:137)
02-04 16:09:29.623: W/System.err(7163):     at android.app.ActivityThread.main(ActivityThread.java:4998)
02-04 16:09:29.633: W/System.err(7163):     at java.lang.reflect.Method.invokeNative(Native Method)
02-04 16:09:29.633: W/System.err(7163):     at java.lang.reflect.Method.invoke(Method.java:515)
02-04 16:09:29.633: W/System.err(7163):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-04 16:09:29.633: W/System.err(7163):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-04 16:09:29.633: W/System.err(7163):     at dalvik.system.NativeStart.main(Native Method)
02-04 16:09:29.633: W/System.err(7163): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
02-04 16:09:29.633: W/System.err(7163):     at libcore.io.Posix.open(Native Method)
02-04 16:09:29.633: W/System.err(7163):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
02-04 16:09:29.633: W/System.err(7163):     at libcore.io.IoBridge.open(IoBridge.java:393)
02-04 16:09:29.633: W/System.err(7163):     ... 16 more

当我在Activity(非异步)中使用它时,相同的代码有效:

public class MyActivity extends Activity{
    ...
    private void buttonPressed(){
            try{
                MediaPlayer=new MediaPlayer();
                File f = new File("/system/media/audio/notifications/Upsilon.ogg");
                FileInputStream fileInputStream = new FileInputStream(f);
                mMediaPlayer.setDataSource(fileInputStream.getFD());
                fileInputStream.close();            
                mMediaPlayer.prepare();
                mMediaPlayer.start();
            }
            catch(Exception e){e.printStackTrace();}
    }
}

清单:                   
                 

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <service
            android:name="com.example.examplepack.MyService"
            android:enabled="true"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"

            >
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" >
                </action>
            </intent-filter>

            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/accessibilityservice" />

        </service>

        <activity
            android:name="com.example.examplepack.MyActivity"
            android:label="@string/title_activity_log" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 

感谢您的帮助

0 个答案:

没有答案