How to start an Intent for an Application Context - android

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?

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);

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

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>

Related

How to start a bound service via intent without class

I'm trying to start a bound (eventually remote) Android Service via an Intent. If I create the Intent with a reference to the Service class (see variable ii below) it works. If I create the Intent using Strings for the classname, bindService fails.
Ultimately I'm aiming to have a 2nd application access my service which will not have the Service class within it (that's how remote service things work...? isn't it?) - hence the desire to create the Intent without the reference to the class object.
//this Intent works
Intent ii = new Intent(this, MyService.class);
//this Intent doesn't
Intent i = new Intent();
i.setClassName("com.ghee", "MyService");
boolean b = bindService(ii,
mSvcConn,
Context.BIND_AUTO_CREATE);
if(!b) {
Log.d(TAG, "bindService returned false...");
}
If it helps, here's the majority of the manifest:
<application android:icon="#drawable/icon">
<activity android:name=".MyActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="com.ghee.MyService" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"
android:exported="true"
android:process=":remote">
</service>
</application>
You need to specify the full path of the class, not just its name. This seems counter-intuitive, because you're already specifying the package in parameter 1, but....oh well.
Try this:
i.setClassName("com.ghee", "com.ghee.MyService");

Reload Just the Main Activity not the Launcher Activity

I'm using this code to refresh MainActivity.java when a Refresh button is pressed.
Intent intent = getIntent();
finish();
startActivity(intent);
MainActivity.java has the category default and there is another Launcher activity. So whenever I press the Refresh button the Launcher Activity also starts again. I only need to start the MainActivity class. Below is the manifest
<application
android:allowBackup="true"
android:icon="#drawable/play_icon"
android:label="#string/app_name"
android:theme="#style/CustomActionBarTheme" >
<activity
android:name="com.theanilpaudel.joshilo.FirstScreen"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.theanilpaudel.joshilo.MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
What you are looking for in this case is an explicit Intent that launches MainActivity.
You can do this like so:
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
This will ensure that the Intent only launches MainActivity.
However, I highly recommend re-evaluating why you need to completely restart the Activity to refresh it. There are much cheaper (faster and less resource intensive) methods of updating data displayed in an Activity.
I guess I know the culprit. Try using the following code once.
Intent intent = getIntent();
startActivity(intent);
finish();
As far as I know startActivity() uses context to run. In case if you call finish() before startActivity() that may lead to destroy context of MainActivity and will get the application's context i.e. fires the activity with behavior LAUNCHER or MAIN.
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Let me know if it helps or you still have difficulty reloading activity.
Cheers

How to launch a certain activity of an Android app

I would like to launch, from my app, two specific activities A_Activity and B_Activity from apps Aapp and Bapp
I inserted two buttons and in the two OnClickListener I wrote
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("com.Acompany.Aapp.A_Activity");
ctx.startActivity(intent);
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("com.Bcompany.Bapp.B_Activity");
ctx.startActivity(intent);
Moreover I added to AndroidManifest.xml the following lines
<activity
android:name="com.Acompany.Aapp.A_Activity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.Acompany.Aapp.A_Activity" />
</intent-filter>
</activity>
<activity
android:name="com.Bcompany.Bapp.B_Activity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.Bcompany.Bapp.B_Activity" />
</intent-filter>
</activity>
But my app crashes and in the logcat I read "No Activity found to handle Intent"
Where is my mistake?
EDIT: More precisely the two activities are not in my own app
More precisely the two activities are not in my own app
You should investigate target app's manifest file first, to check if these activities are available to others by being exported or offering publicly accessible intent filters. It looks you may simply be not allowed to do what you attempt to.
Try like this. For your home activity(first launch activity) do like this in your manifest file.xml
<activity
android:name="com.Acompany.LaunchHomeActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.Acompany.LaunchHomeActivity" />
</intent-filter>
</activity>
<activity android:name="com.Acompany.Aapp.A_Activity">
</activity>
<activity android:name="com.Bcompany.Bapp.B_Activity">
</activity>
don't include <intent-filter> to all activities
Intent intent = new Intent(myFirstClass.this, MySecondClassA.class);
startActivity(intent);
Class B
Intent intent = new Intent(myFirstClass.this, MySecondClassB.class);
startActivity(intent);

android how to start the main activity when I am on another activity

I am on signin activity and if the user click a button I want to start the main activity.
this is the mainfest for the main activity welcome
<activity
android:name=".Welcome"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I tried like this
Intent welcomeIntent =new Intent("android.intent.action.MAIN");
startActivity(welcomeIntent);
but i got a screen saying complete acting using with many choese , what is the solution pelase?
Change your intent to directly launch your class
Intent welcomeIntent = new Intent(context, Welcome.class);
startActivity(welcomeIntent);
If you're in an activity replace context with this

launch activities from different package

I have activity A in package one, and I want to run an intent which will up an activity B which is in package two.
How can I do this? Any samples will be welcome.
this is what ive done, and the error i get:
first activity ("MainActivity") in a package: com.abelski.currencyclient
and second activity("SecondActivity" in a diffrent package: com.idan.second
now i wanna call from MainActivity to SecondActivity.
ive added this line at the manifest of the MainActivity:
<activity android:name="com.idan.second.SecondApplicationActivity"></activity>
now in main Activity i got this button which run this line:
Intent intent = new Intent(MainActivity.this,SecondApplicationActivity.class);
and this is the rror:
04-29 09:20:59.197: ERROR/AndroidRuntime(399): Uncaught handler: thread main exiting due to uncaught exception
04-29 09:20:59.276: ERROR/AndroidRuntime(399): java.lang.NoClassDefFoundError: com.idan.second.SecondApplicationActivity
04-29 09:20:59.276: ERROR/AndroidRuntime(399):
I am assuming that by "packages" you mean applications.
We have:
- ApplicationA with FirstActivity
- ApplicationB with SecondActivity
If, in the ApplicationB's AndroidManifest.xml file, in the declaration of SecondActivity you add an intent filter such as:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="applicationB.intent.action.Launch" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You can create an Intent to launch this SecondActivity from the FirstActivity with:
Intent intent = new Intent("applicationB.intent.action.Launch");
startActivity(intent);
What this all means is:
The SecondActivity has a filter for the intent action of "applicationB.intent.action.Launch"
When you create an intent with that action and call 'startActivity' the system will find the activity (if any) that responds to it
The documentation for this is at: https://developer.android.com/reference/android/content/Intent.html
I had the same problem and the solution was another level in the root of your package name.
If you have two packages "com.first...." and "com.second...", and the manifest is referencing "com.first". Then you can refactor both packages in order to reuse the first part. For instance, "com.package.first" and "com.package.second". Then your manifest has to be also updated
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package">
...
<activity android:name=".first.FirstPackageActivity" android:label="FirstPackageActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".second.SecondPackageActivity" android:label="SecondPackageActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Your java code can create an intent and start the activity in the usual way:
Intent intent = new Intent(this,ActivityClassName.class);
startActivity(intent);
If the package you mentioned here is same to application,I think the answer in the question Android: Starting An Activity For A Different Third Party App is simpler。With the first answer to that question, you don't need to make any modification to your second application.
Use explicit intents:
Intent intent = new Intent(context,ClassName.class);
where ClassName is from another package.
Sometimes, you will not know the name of the class in such cases you will have to rely on the Intent that the target class advertises to handle.
First, you need to declare both packages and activities on Manifest :
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
and for the second activities, on android:name --> .Packages name.activity, assuming your second packages name com.iden.second :
<activity android:name=".second.SecondActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<intent-filter>
<action android:name="android.intent.action.SECOND" />
<category android:name="android.intent.category.SECOND" />
</intent-filter>
</activity>
then on the MAINACTIVITY JAVA CLASS, asuming you will start second activity using a button :
public class MainActivity extends AppCompatActivity {
private Button mButtonSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonSecond = findViewById(R.id.btn_second);
mButtonSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent newActivity = new Intent(MainActivity.this,com.iden.second.SECOND.class);
startActivity(newActivity);
}
});
}
}
hope that can help, since i am not clear with the question structure

Categories

Resources