ActivityNotFoundException无法解析

时间:2015-03-29 23:28:36

标签: java android

我想通过广播接收器打开活动,我尝试了任何方式,但我得到ActivityNotFoundException,我的活动正在正常模式,但当我想从BroadCastRecevier打开它时它会导致ActivityNotFoundException错误,

这是我的表现,

<activity
    android:excludeFromRecents="true" 
    android:launchMode="singleTask"
    android:screenOrientation="portrait"
    android:name="com.alexis.abc.ui.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

这是我的广播接收器,

Intent intent2 = new Intent(context, MainActivity.class);
intent2.addCategory("android.intent.category.LAUNCHER");
intent2.setAction("android.intent.action.MAIN");
context.startActivity(intent2);

以下是步骤:
1 - 我通过以下代码打开应用程序并隐藏启动器图标

PackageManager packageManager = getContext().getPackageManager();
ComponentName componentName = new ComponentName(getContext(), MainActivity.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

2 - 我退出应用程序并拨打号码(触发广播事件),我得到以下异常

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.alexis.abc/com.alexis.abc.ui.MainActivity}; have you declared this activity in your AndroidManifest.xml?

2 个答案:

答案 0 :(得分:0)

你有这样的许可:

<receiver
android:name="Your receiver"
android:enabled="true"
android:exported="false" >
   <intent-filter>
       <action android:name="action"/>
       <category android:name="category" />
   </intent-filter>
</receiver>

在Android Manifest文件中?如果没有,请尝试打开您的Android清单文件并添加我上面发布的内容。

或者在代码的最后一行尝试.sendBroadcast()而不是.startActivity():

Intent intent2 = new Intent(context, MainActivity.class);
intent2.addCategory("android.intent.category.LAUNCHER");
intent2.setAction("android.intent.action.MAIN");
context.sendBroadcast(intent2);

答案 1 :(得分:0)

更改要打开应用程序的代码并隐藏启动器图标:

ComponentName componentName = new ComponentName(getContext(), YourReceiver.class);

不要忘记在清单中声明BroadcastReceiver

<receiver
   android:name="YourReceiver" >
   <intent-filter>
       <!-- your intent filter goes here -->
   </intent-filter>
</receiver>