3

I found almost the same question:How to start an Intent if context is not Activity Context but Application Context

but I faild to do it when using 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

    <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?

Community
  • 1
  • 1
eason
  • 55
  • 1
  • 9

3 Answers3

2

You can not start an Application class. You can only start an Activity.

Change your code from e.g.:

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

to

Intent applicationIntent = new Intent(context, myCam.class);
Christopher
  • 9,682
  • 7
  • 47
  • 76
  • I'm not sure if it's possible, here is a similiar question: http://stackoverflow.com/questions/9237448/how-to-start-an-intent-if-context-is-not-activity-context-but-application-contex – eason Jul 15 '16 at 09:40
  • I'm not sure, what are you trying to achieve? There is no way to start an Application-class via startActivity! – Christopher Jul 15 '16 at 09:43
  • I want to do something only one time in the Application's contructor when boot commpleted – eason Jul 15 '16 at 09:46
  • I think the application-class is already instantiated, if your BroadcastReceiver gets a message? Otherwise you won't have an context instance, or not? – Christopher Jul 15 '16 at 09:52
1

Try to create an other Activity and add it to your Manifest.xml and see if there's any error do not forget to add this code <activity android:name=".yourSecondActivity" /> and start a new Intent

Yagami Light
  • 1,756
  • 4
  • 19
  • 39
  • 1
    I could succeed to start an activity context by context.startActivity. but couldn't do it for an Application class – eason Jul 15 '16 at 09:27
  • did you use `Intent intent = new Intent(MainActivity.this, yourSecondActivity.class);MainActivity.this.startActivity(intent);` ??? – Yagami Light Jul 15 '16 at 09:30
  • I have included the code, please have a look, thanks – eason Jul 15 '16 at 09:34
1

The problem here is that you don't understand basic principles. In android you have only 4 components: 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. 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);

P.S. Your Application class will start automatically. Lifecycle of application is following: Start of Application class -> Start of Component(service, activity..) -> Stop of Component(service...) -> Stop of Application class

P.S.S. There is no need to check the action in onReceive - it will be there for the only reason:

<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
Viktor Yakunin
  • 2,927
  • 3
  • 24
  • 39