简体   繁体   English

如何启动应用程序上下文的Intent

[英]How to start an Intent for an Application Context

I found almost the same question: How to start an Intent if context is not Activity Context but Application Context 我发现了几乎相同的问题: 如果上下文不是活动上下文而是应用程序上下文,如何启动Intent

but I faild to do it when using https://stackoverflow.com/a/9238105/6593395 但是我在使用https://stackoverflow.com/a/9238105/6593395时会这样做

public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if ("android.intent.action.BOOT_COMPLETED".equals(action)) {
        Intent applicationIntent = new Intent(context, myCamApplication.class);
        applicationIntent.setAction(myCamApplication.class.getName());
        applicationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(applicationIntent);

the error log: 错误日志:

Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.fenchtose.asyncamera/com.eason.mycamera.myCamApplication}; have you declared this activity in your AndroidManifest.xml?

I have registered this myCamApplication class as my application class inside AndroidManifest.xml 我已将此myCamApplication类注册为AndroidManifest.xml中的应用程序类

    <application
    android:name=".myCamApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    >
    <receiver
        android:name=".BootComplete"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <activity
        android:name=".myCam"
        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>

So, Anyone could help? 那么,任何人都可以帮忙吗

You can not start an Application class. 您无法启动Application类。 You can only start an Activity. 您只能启动活动。

Change your code from eg: 从以下地址更改您的代码:

Intent applicationIntent = new Intent(context, myCamApplication.class);

to

Intent applicationIntent = new Intent(context, myCam.class);

尝试创建另一个Activity并将其添加到您的Manifest.xml ,看看是否有任何错误,不要忘记添加此代码<activity android:name=".yourSecondActivity" />并启动一个新的Intent

The problem here is that you don't understand basic principles. 这里的问题是你不了解基本原则。 In android you have only 4 components: Activity, Service, ContentProvider, BroadcastReceiver. 在android中你只有4个组件:Activity,Service,ContentProvider,BroadcastReceiver。 So in your manifest you declare that you have Application (name, attributes...) and inside the application you have Activities, Services, Receivers, ContentProviders. 因此,在您的清单中,您声明您拥有应用程序(名称,属性......),并且在应用程序中您拥有活动,服务,接收者,内容提供者。

Your application is not your Application class. 您的应用程序不是您的Application类。 So to start your application you should start default activity: 因此,要启动应用程序,您应该启动默认活动:

Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//because we use Application context
startActivity(intent);

PS Your Application class will start automatically. PS您的Application类将自动启动。 Lifecycle of application is following: Start of Application class -> Start of Component(service, activity..) -> Stop of Component(service...) -> Stop of Application class 应用程序的生命周期如下:应用程序类的开始 - >组件的开始(服务,活动..) - >组件的停止(服务...) - >停止应用程序类

PSS There is no need to check the action in onReceive - it will be there for the only reason: PSS没有必要检查onReceive中的操作 - 它将存在于唯一的原因:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM