When I have e.g. a custom service and a custom activity in the same ADT project, then I can use this in the service, to start my activity:
Intent i = new Intent(context, MyCustomActivity.class);
startActivity(i);
However when I have the service and activity in separate projects, then I cannot do this since I do not have a direct reference to MyCustomActivity.class. This is problematic: I do not wish to include a JAR just to be able to fix that broken reference, since I assume this will increase the package size and create redundant data on the device (i.e. activity code is duplicated between the service and activity packages). So instead, I use this (perhaps there are other options?):
Intent i = new Intent("com.mypackage.myStringActionName");
startActivity(i); //is this a broadcast?
OR
Intent i = new Intent("com.mypackage.myStringActionName");
sendBroadcast(i);
...But I don't really like sending broadcasts when all I want is to direct the intent to a single activity to tell it to start.
So, what other ways are there to go about avoiding duplication (in ADT)? Or else a better way to send direct intents?
you can try this:
Intent i = new Intent();
i.setComponent(new ComponentName(packageName, classname));
startActivity(i);
the className must contains the packageName and main activity name
Related
For what purpose Context class is used in android?Please explain me in depth and be more specific .I read all other posts but none of them were specific enough to give me clear understanding.
I know Content class allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
Like Here,
Intent intent=new Intent(this,new_class.class);
why we are passing Main activity context into the Intent constructor call.what type of information does this activity context contain,how will it help it ,what type of resource access is it providing to it ?(with example please).
Similarly,
here,
TextView textview=new TextView(this);
Why TextView need activity context?How does it help it.
There are already several good explanations of Context on Stackoverflow (see the linked questions and us the "search" feature. Also, the source code for Android is available on grepcode.com and you can look yourself if you are really interested. Why trust someone else's answer if you can look yourself? ;-)
However, I will answer your specific questions:
Intent intent=new Intent(this,new_class.class);
why we are passing Main activity context into the Intent constructor
call.what type of information does this activity context contain,how
will it help it ,what type of resource access is it providing to it
?(with example please).
In this case (the 2-argument constructor for Intent), the Context parameter is only used to determine the package name of the target Activity. Assuming that the package name of your application is "com.example.app", and MyActivity is an `Activity of your application, the following code snippets are all functionally identical:
Intent intent = new Intent(this, MyActivity.class);
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
Intent intent = new Intent();
intent.setComponent(new ComponentName(this, "com.example.app.MyActivity");
Intent intent = new Intent();
intent.setComponent(new ComponentName(this, MyActivity.class);
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app", MyActivity.class);
Intent intent = new Intent();
intent.setClass(this, MyActivity.class);
Intent intent = new Intent();
intent.setClassName("com.example.app", "com.example.app.MyActivity");
Similarly, here,
TextView textview=new TextView(this);
Why TextView need activity context?How does it help it.
All Views need a Context. Think of the Context as the "owner of the View". This controls the lifetime of the View. In general, a View should have the same lifetime as its owning Activity, which is why you usually pass the Activity as the Context parameter when creating a View. When the Activity is destroyed, all of the owned Views are also destroyed. Additionally, the View uses the Context to gain access to resources (drawables, layouts, strings, themes, etc.).
It act as a Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
You can get all the information from delveloper's official documentation... https://developer.android.com/reference/android/content/Context.html
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.
I m creating an app which opens another app in the background and my first app will be sending some string as a data to another app, so can I send Intents to another app's like we usually send intents to other classes containing data?
If we can then how can I send it?
Yes , you can send the intent to any app you like but its upto the receiving application to handle it.
Few apps may crash receiving it.
The way
Make an intent
Intent i=new Intent(yourContext,Activity_to_which_you_to_send.class);
Put some data-if you want to
i.putExtraString("key","value");
or put using a bundle
Bundle b=new Bundle();
b.putString(key,boolean_value);
b.putBoolean(key,boolean_value)
Starting the activity
startActivity(i);
Set the package of the app
i.setPackage("com.whatsapp");
Example
if you want to find out the main Activity of an app
go to command line
type adb shell pm -lf
pick any one and try it by passing as a second argument to the intent constructor defined above and then call startActivity method.
hope it helps you.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("text/plain");
intent.putExtra(name,yourString);
try{
startActivity(intent);
} catch (Exception ActivityNotFoundException){
ActivityNotFoundException.printStackTrace();
}
Use this code to send an intent to another app and specify the name value and string in putExtra function.
Yes you can send Intent to another app like we send to other classes with passing data as a string so you can see it HERE
Yes. "App" is not so well defined on Android, it's a loose term. Intents are used to start an Activity, among other things.
What is the difference between starting ActivityB from ActivityA using
1. startActivity(this, ActivityB.class);
versus
2. startActivity(getApplicationContext(), ActivityB.class);
I typically see 1. used more often in examples, but I haven't come across a reason for why this is the case.
Reference to Activity as a Context (this) might become obsolete if your Activity goes through configuration changes, like rotation, and is destroyed and created again. Context recieved by getApplicationContext(), however, persists through lifetime of the process.
However, It seems to me it only is an issue when you bind Activity to Service or other similar scenario, so it's safe to use this when you use it in intent to start another Activity.
There is no difference. According source code of Intent and ComponentName - only thing, that used form context - is getting package name by context.getPackageName(). Package name is the same for Activity.this and Activity.getApplicationContext(), so there is no difference.
I assume you are actually asking about the difference between
startActivity(new Intent(this, ActivityB.class));
and
startActivity(new Intent(getApplicationContext(), ActivityB.class));
There is no difference. Android needs the ComponentName (package name and class). The context is used to determine the package name.
My boss asked me to prove that my application behaves properly when summoned by another application (dunno why he asked that).
So I have two apps here, one launches a second one. How I launch the specific app I want? Using Intent launch seemly any generic app that reaches a certain goal, not the app I really want.
Give this a try.
Intent secondIntent = new Intent();
secondIntent.setAction(Intent.ACTION_MAIN);
secondIntent.setClassName("com.example", "com.example.YourSecondApp");
startActivity(secondIntent);
I should point out that com.example should be the package of your second application (the one you want to call) and com.example.YourSecondapp is the class name where you have your onCreate() method.
Intent secondApp = new Intent("com.test.SecondApp");
startActivity(secondApp);
Check out for more examples
http://developer.android.com/resources/faq/commontasks.html#opennewscreen
Create one Intent using the following code
Explicit Intent
When you know the particular component(activity/service) to be loaded
Intent intent = new Intent();
intent.setClass("className/package name");
start<Activity/Service>(intent);
Imlicit Intent
When we do not have the idea which class to load and we know the Action to be perform by the launched application we can go with this intent.
Action needs to set, and the Android run time fallows the intent Resolution technique and list out(one or more components) the components to perform the action. from the list out components (if more than one), user will get the chance to launch his chosen application