Try to start library project Activity

非 Y 不嫁゛ 提交于 2019-12-14 01:09:21

问题


I have a library project and Main project and I try to start the activity that is defined in library project from main project.

Intent intent = new Intent("isr.LAUNCH");
intent.setComponent(new ComponentName("com.ISR", "com.ISR.activity.CameraActivity"));
startActivity(intent);

But I receive the following exception:

05-14 17:13:42.853: E/AndroidRuntime(29217): java.lang.SecurityException: Permission Denial: starting Intent { act=isr.LAUNCH cmp=com.ISR/.activity.CameraActivity } from ProcessRecord{40aa7178 29217:com.ssbs.sw.SWE/10094} (pid=29217, uid=10094) requires null
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.os.Parcel.readException(Parcel.java:1322)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.os.Parcel.readException(Parcel.java:1276)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1351)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1374)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.app.Activity.startActivityForResult(Activity.java:2827)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.app.Activity.startActivity(Activity.java:2933)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at com.ssbs.sw.IR.view.MainActivity$1$1.onMenuItemClick(MainActivity.java:58)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at com.ssbs.sw.SWE.actionbarV5.menu.MenuButtonHelper$1.onClick(MenuButtonHelper.java:108)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.view.View.performClick(View.java:2492)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.view.View$PerformClick.run(View.java:9087)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.os.Handler.handleCallback(Handler.java:587)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.os.Looper.loop(Looper.java:130)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at android.app.ActivityThread.main(ActivityThread.java:3740)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at java.lang.reflect.Method.invokeNative(Native Method)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at java.lang.reflect.Method.invoke(Method.java:507)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
05-14 17:13:42.853: E/AndroidRuntime(29217):    at dalvik.system.NativeStart.main(Native Method)

This is my manifest from library project:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <activity android:name=".activity.CameraActivity" android:exported="true" >
            <intent-filter>
                <action android:name="isr.LAUNCH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

How can I fix this?

EDIT
My main project manifest permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

EDIT 2
I have resolved my problem by coping to my main application manifest definition of called activity (but I would like to know if I can do this without adding this definition):

<activity android:name="com.ISR.activity.CameraActivity">
            <intent-filter>
                <action android:name="isr.LAUNCH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

And now I can start my activity with following code:

Intent intent = new Intent("isr.LAUNCH");
startActivity(intent);

回答1:


The manifest for your main app must contain all Activity definitions it wishes to use. They are not required in the library project as they are not called from there.

Any activity definitions you have in your library manifest actually do nothing at all.

Update: You can now merge manifest files as other answers have mentioned.




回答2:


You can actually declare everything you want in the library's Manifest and then in your main project declare

manifestmerger.enabled=true

In your project.properties. What it does is actually merge your Manifests and you can see the resulting Manifest in your bin folder.



来源:https://stackoverflow.com/questions/16545725/try-to-start-library-project-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!