use*_*538 13 android android-intent android-service android-permissions
我已将服务编写为应用程序的一部分.我尝试使用以下代码从第二个应用程序调用此服务:
Intent intent = new Intent () ;
intent.setClassName("com.test" ,"com.test.DownloadService") ;
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(handler);
intent.putExtra("MESSENGER", messenger);
intent.setData(Uri.parse("http://www.vogella.com/index.html"));
intent.putExtra("urlpath", "http://www.vogella.com/index.html");
this.startService(intent);
Run Code Online (Sandbox Code Playgroud)
它给出了以下例外
07-10 09:27:28.819: ERROR/AndroidRuntime(4226): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.inject/com.inject.MyActivity}: java.lang.SecurityException: Not allowed to start service Intent { dat=http://www.vogella.com/index.html cmp=com.test/.DownloadService (has extras) } without permission not exported from uid 10109
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1971)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1996)
at android.app.ActivityThread.access$700(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1156)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4458)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Not allowed to start service Intent { dat=http://www.vogella.com/index.html cmp=com.test/.DownloadService (has extras) } without permission not exported from uid 10109
at android.app.ContextImpl.startService(ContextImpl.java:1122)
at android.content.ContextWrapper.startService(ContextWrapper.java:359)
at com.inject.MyActivity.onCreate(MyActivity.java:28)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1935)
... 11 more
Run Code Online (Sandbox Code Playgroud)
为什么会出现此权限问题?
当我从同一个应用程序调用服务时,以下代码非常有用:
Intent intent = new Intent(this, DownloadService.class);
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(handler);
intent.putExtra("MESSENGER", messenger);
intent.setData(Uri.parse("http://www.vogella.com/index.html"));
intent.putExtra("urlpath", "http://www.vogella.com/index.html");
startService(intent);
Run Code Online (Sandbox Code Playgroud)
有人请告诉我为什么当我从其他应用程序调用该服务时,我收到此权限问题,尽管我已在App2的清单中提供了所有必需的权限.
编辑
App2的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inject"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="14"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
包含服务的App1的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="14"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<service android:name="DownloadService" >
</service>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
Dav*_*ser 16
该服务不可用于其他应用程序,因为您尚未明确提供该服务.您的服务需要像这样定义:
<service android:name="DownloadService"
android:exported="true">
</service>
Run Code Online (Sandbox Code Playgroud)
您不需要任何额外的权限(除非您想使用它们)
您阅读过文档吗?这个问题其实说的很清楚了:
当在清单标签中声明服务时,可以强制执行对服务的全局访问。通过这样做,其他应用程序将需要在自己的清单中声明相应的元素,以便能够启动、停止或绑定到服务。
从 GINGERBREAD 开始,当使用 Context.startService(Intent) 时,您还可以在 Intent 上设置 Intent.FLAG_GRANT_READ_URI_PERMISSION 和/或 Intent.FLAG_GRANT_WRITE_URI_PERMISSION 。这将授予服务临时访问 Intent 中的特定 URI 的权限。访问将一直保留,直到服务为该启动命令或后续命令调用 stopSelf(int),或者直到服务完全停止。这适用于向未请求保护服务的权限的其他应用程序授予访问权限,甚至在服务根本未导出时也是如此。
此外,服务可以通过在执行该调用的实现之前调用 checkCallingPermission(String) 方法来保护对其进行的各个 IPC 调用的权限。
有关一般权限和安全性的更多信息,请参阅安全和权限文档。
AndroidManifest.xml
如果您在阅读文档后仍然遇到权限问题,请告诉我们您的情况。另请参阅清单中服务的exported
和属性。permission
归档时间: |
|
查看次数: |
23690 次 |
最近记录: |