Reputation: 55
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?
Upvotes: 3
Views: 3566
Reputation: 3266
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>
Upvotes: 1
Reputation: 10259
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);
Upvotes: 2
Reputation: 1796
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
Upvotes: 1