Launching Activity from another App? (with AndroidAnnotations) - android

How can i launch an AndroidAnnotations Activity_ inside of an App (main)
since external Activity (another App).
this is my current code:
Intent codeScannerActivity = new Intent(PACKAGE, CODE_SCANNER_ACTIVITY);
codeScannerActivity.putExtra("codeScannerType", CameraUtils.CODE_SCANNER_SINGLE);
startActivityForResult(codeScannerActivity, Core.ActivityResult.RequestCode.CODE_SCANNER);
where PACKAGE = "main.app.package"
and CODE_SCANNER_ACTIVITY = PACKAGE + ".activity.MyActivity_"
but logs throws:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=main.app.package dat=main.app.package.activity.MyActivity_ (has extras) }
Activity is defined in the Manifest's Main App with the Class "etc.MyActivity_".

You are constructing the Intent incorrectly. For the constructor you are using, the first parameter is interpreted as an "action" and the second as a URI. The error says that there is no activity which can respond to the action "main.app.package" and the URI "main.app.package.activity.MyActivity_".
To fix the problem, first read Starting Another Activity and the Intent javadocs from the Android Developer site. Especially look at the documentation for the available constructors. There might be one more appropriate for your purposes than the one you are trying to use. The Intent documentation has a list of standard Activity actions. If you want to start a specific activity, you should use Intent (Context packageContext, Class<?> cls):
Intent intent = new Intent(this, main.app.package.activity.MyActivity_.class);

I was creating wrong the Intent, this is the right way:
Intent codeScannerActivity = new Intent();
codeScannerActivity.setComponent(new ComponentName(PACKAGE, CODE_SCANNER_ACTIVITY));
codeScannerActivity.putExtra("codeScannerType", CameraUtils.CODE_SCANNER_SINGLE);
startActivityForResult(codeScannerActivity, Core.ActivityResult.RequestCode.CODE_SCANNER);

Related

How to run activity from 3rd party library?

I am in a non-activity class. In this place I want to run the activity from a 3rd party library which I attached to my project. I have written some code which I thought will do it:
Intent intent = new Intent();
intent.putExtra(THIRD_PARTY_ACTIVITY.REPORT_EXTRA, parts.toString());
Activity activity = new THIRD_PARTY_ACTIVITY();
activity.startActivity(intent);
This code compiles successful but when I execute it, I get the error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'android.app.ActivityThread$ApplicationThread
android.app.ActivityThread.getApplicationThread()' on a null object
reference
How can I run activity from a 3rd party library?
You're doing it wrong. You NEVER create an Activity with new. It won't initialize correctly. Instead, you create an Intent to launch that Activity and call context.startActivity() to create and launch it.
void launchThirdPartyActivity(Context context) {
Intent intent = new Intent(context, THIRD_PARTY_ACTIVITY.class);
context.startActivity(intent);
}
That should work if the activity is in a library. If you're trying to launch an Activity in another app on the device, you'd use one of the other Intent constructors (which one depends on how/what you're trying to launch- a specific activity in a specific app? An activity that can perform an action (like share or view) on a specific data type? Something else?)
In your library you can do this:
Intent intent = new Intent();
// Here you need to set the ACTION or the COMPONENT in the Intent
// so that Android knows which Activity you want to start
// Also, you need to set FLAG_ACTIVITY_NEW_TASK because you aren't calling
// startActivity() on an Activity Context
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(THIRD_PARTY_ACTIVITY.REPORT_EXTRA, parts.toString());
Application.getApplicationContext().startActivity(intent);

How to start an Intent in Android

I just following a tutorial to start a basic intent, and I get an error. Doing exact the same as the tutorial, created an empty Activity and just passed as argument to the intent. What is the problem?
Using a video tutorial: Lynda.com - Developing Android Apps Essential Training (2015)
The error you've got in IDE usually happens when you try to use Activity Context (keyword this) inside some callback, listener or anonymous function. In such situation this does not refer to the Context of the Activity.
That's why solution provided by #Vishal Patoliya should fix your problem because you're explicitly referring to the Context of the concrete Activity as follows:
Intent intent = new Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
Try this
Intent intent = new Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
startActivity(intent);
In case of Activtiy, you are going to another activity:--
Intent intent = new
Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
startActivity(intent);
If you are passing intent from a fragment to open a activity then
Intent intent = new Intent(getActivity(),ItemUserSettingRattingActivity.class);
startActivity(intent);
Have you already created a class named ItemUserSettingRattingActivity.java? maybe you get an error it's because the intent you created didn't detect the class.

Android request to another service - explicit vs implicit intent

I am trying to make a request to another service (another app), but getting exception:
java.lang.IllegalArgumentException:
Service Intent must be explicit: Intent { act=com.myApp.DoAction }
I did some readings and still confused:
Here, it's written: Since Android 5.0 (Lollipop) bindService() must always be called with an explicit intent.
Here, it's written: Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it
What will be the right intent? What will be the right example?
I am doing:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setAction("com.anotherApp.MyService");
// binding to remote service
bindService(intent, AddServiceConnection, Service.BIND_AUTO_CREATE);
Many many thanks for help.
I believe that this
Intent intent = new Intent(Intent.ACTION_SEND);
line of code says that intent action is implicit. It declares intent with abstract SEND action. try removing Intent.ACTION_SEND
After trying some tutorials:
Intent is implicit and this nice example shows how to deal with it
Intent intent = new Intent("com.anotherapp.MyService");
intent.setPackage("com.anotherapp");
bindService(intent, AddServiceConnection, Service.BIND_AUTO_CREATE);

Different types of Android Intents

I have recently started a new Android project and I'm working off the previous developer's code. I'm relatively new to Android and I've come across something that I'm unsure of.
What is the difference between this:
Intent intent = new Intent("com.example.project.MENU");
and this:
Intent intent = new Intent(this, DisplayMenu.class);
I understand what the 2nd code snippet does, I just can't get my head around as to what the first one is doing? Is it referencing the file in the package? Thanks
The first one is an implicit intent, while the second is an explicit intent.
The first one fired an Intent for the action com.example.project.MENU. If you look inside you project AndroidManifest.xml you can see some <intent-filter> balise. This baslise register activity, service or broadcast receiver to different actions.
This mecanism can be used to allow third party app to launch some of your activities.
You can see more on this tutorial http://www.vogella.com/tutorials/AndroidIntent/article.html#intenttypes
Basically an Intent carries some information that are used by the system in order to determine which component should be called for executing the action.
These information are:
Component name: the name of the component that should be launched. (If present the Intent is Explicit)
Action: it specifies the generic action that should be executed (es. ACTION_VIEW, ACTION_SEND). It determines how the rest of the intent is strucutred.
Data: represents the URI that refers to the object that should be associated with the action. For example with the action ACTION_EDIT, the Data should contain the URI of the document that you want modify.
Category: Additional infromation (for example if you want that your app is shown in the launcher you can use CATEGORY_LAUNCHER)
Extras: keys-values pairs that carries additional information
Flags: it is like a metadata that specify how the intent should be managed by the system.
The Intent class provides a lot of different constructors.
The first one you asked for is public Intent (String action)
So, this sets the Action, and lets null all other fields.
The second one public Intent (Context packageContext, Class<?> cls) creates an intent for a specific component by its Component name. All other fields are null. This is a Explicit Intent, since you declare exactly which component should receive it.
The first one is used when you need to call Intent from System
such as Open Camera, Gallery, or Share something to other Application
for example
// this one call Camera to Capture Image
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// this one call gallery to let you select image
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
and That MediaStore.something here is just a Path to the system
for example
MediaStore.ACTION_IMAGE_CAPTURE = "android.media.action.IMAGE_CAPTURE"
Intent.ACTION_PICK = "android.intent.action.PICK"
The first type of intent is mostly used if you want to open another application from your application while the second type of intent is used to open another activity in your application.

difference between intent and intenfilter in android

Hi friends
can anybody tell what is the use of intent filter and what is difference between intent and intent filter
Thanks
An Intent is an object passed to Context.startActivity(),Context.startService() or Activity.startActivityForResult() etc. to launch an activity or get an existing activity to do something new.
While an Intent filter describes a capability of the component(like activities, services, and broadcast receivers )
For more details see this
Intent's are used to start new activity from the current activity. With two ways
1) Activity.startActivity(intent);
2) Activity.startActivityForResult(intent,rqwuestCode);
//The above thing's you need to put in .java file
Intent-filter you need to mention on manifeast file.
// Intent filter are used for broadcast receiver. Whenever the intent filter condition is match the android OS will launch that activity.
An intent is an object that can hold the os or other app activity and its data in uri form.It is started using startActivity(intent-obj)..\n whereas IntentFilter can fetch activity information on os or other app activities.

Categories

Resources