Pass information from remote service to application - android

I created my own custom browser and I also have a background service that runs on startup. These are not in the same package, they are two separate installs. I want the service to be able to open my custom browser and launch a specific website in it at a specific time. I am currently able to launch the custom browser from the service but I don't know how to pass the specified url to it. Is this possible?
EDIT
I currently have it working now using something along the lines of this in my background service.
intent.putExtra("WebSite", "www.android.com")
then in my custom browser I put this in the onCreate() method
Intent sender = new Intent();
sender = getIntent();
String address = sender.getExtras().getString("WebSite");
I am getting the url then, but it is obviously force closing when I launch the app on my own instead of letting the remote service launch it because there is no intent for getIntent to get. I'm going to put a method in to check if there is an intent and if there is to launch it and if not skip. I think that should work. does anyone else have a better idea?

First activity (thus your service):
Intent myIntent = new Intent();
myIntent.putExtra("website", "www.android.com");
startActivity(myIntent);
Second activity (thus your browser:)
String website = "";
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
website = bundle.getString("website");
}

Related

getExtras() return null on Application level (-class)

On my Application level I receive null for getExtras(), but on Activity level i can see them correctly.
public class MyApplication extends Application
{
#Override
public void onCreate() {
super.onCreate();
Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.MyApp");
if (intent != null){
String mStaticWorldUrl = intent.getStringExtra("arg1Name");
String mStaticWorldIconUrl = intent.getStringExtra("arg2Name");
Log.i("LOG", mStaticWorldUrl + " --- " + mStaticWorldIconUrl);
}
}
}
I'm calling the app from some shortcuts that were created by this code:
(- each shortcut has different Extras sent to the Intent)
// create a shortcut for the specific app
public static void createShortcutForPackage(Context context,
String packageName, String className, String shortcutName,
String arg1Name, String arg1Val, String arg2Name, String arg2Val,
int iconID) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, className));
PackageManager pm = context.getPackageManager();
Context pkgContext = createPackageContext(context, packageName);
if (pkgContext == null)
return;
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
Intent shortcutIntent = pm.getLaunchIntentForPackage(packageName);
if (arg1Name != null)
shortcutIntent.putExtra(arg1Name, arg1Val);
if (arg2Name != null)
shortcutIntent.putExtra(arg2Name, arg2Val);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context, iconID));
shortcut.putExtra("duplicate", false);
context.sendBroadcast(shortcut);
}
how can I read these Extras on the Application level?
or is there any other way to create different shortcuts for application and read its' "parameters" data on Application?
The Application class is static for the application: there is only ever a single instance of it for your app's process. If your app has been launched with a normal launch Intent, rather than a shortcut you created, then no extras would be present. The app process does not die when HOME or BACK is pressed, so the Intent used to launch the package may not be what you think it should be.
You should not need to look at the Intent at the Application level. Intent objects are not intended to be "sent" there, but rather to an Activity, Service or BroadcastReceiver.
This is conceptual error which facing to get data in application class using getExtra which is the method of Intent
Reason for this issue:
No extras is because the queried intent was one that was generated
one line earlier, it is the intent that the OS has generated for the
package as requested in the following code: Intent intent =
getPackageManager().getLaunchIntentForPackage("com.example.M‌​yApp");
Intent objects within an Application class instance: they are not
delivered there
Let's understand following things to use in upcoming usage while anyone want to get data in application level
What is Intent ?
What is the Use of Intent?
What other things can we use to achieve this?
What is Intent?
An Intent provides a facility for performing Late runtime binding between the code in different applications . Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
extras - This is a Bundle of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.
What is the Use of Intent?
Use to intents facilitate communication between components in several ways, Followings are standard use
To start an activity.
To start a service.
To deliver a broadcast
What other things can we use to achieve this?
There are lots of things we can use to achive this and solved this
issue.
But right now here i mentioned only one which is standard and secured
to use application
Content provider: To offer a file from your app to another app is to
send the receiving app the file's content URI and grant temporary
access permissions to that URI. Content URIs with temporary URI
access permissions are secure because they apply only to the app that
receives the URI, and they expire automatically.

Detect if user starts an application, android

Thanks in advance for the help.
I have an app that can be started by either the user physically starting the app (like you would any normal app) or by a repeating service. Depending on what starts the app (the user or the service) I want to preform different initialization actions. How might I be able to detect if an user starts the app without doing anything custom (I imagine that there has to be some kind of built in setting in android for me to determine this)?
If service, that starts your Activity, is yours service, you can put some custom information (using Intent#putExtra for example) in Intent you use to start Activity from Service.
In Activity you can use Activity#getIntent(), that returns the intent that started this activity.
If you started Activity from Service, that Intent will be the one you passed in Service#startActivity, and will have your custom information. Otherwise, that was not your Service, that started your Activity.
That could look somehow like that, for example:
//in Activity
public static final String EXTRA_STARTED_FROM_MY_SERVICE = "com.example.extra_started_from_sevice";
private boolean wasActivityStartedFromService() {
Intent startingIntent = getIntent();
//assuming you will use Intent#putExtra in your service when starting activity
return startingIntent.getBooleanExtra(EXTRA_STARTED_FROM_MY_SERVICE, false);
}
//...
//in Service
//...
Intent startingIntent = new Intent(this, MainActivity.class);
startingIntent.putExtra(MainActivity.EXTRA_STARTED_FROM_MY_SERVICE, true);
startActivity(startingIntent);

Can I send Intent to another app

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.

How to re-send intent?

I have intent associated with my application.
Eg. Link to an web page.
Sometimes my application can't handle intent and should redirect it to other application (web browser).
The bottom line is that query parameters in link can't be distinguished from another one.
How can I do that?
You can get the Intent that was sent to your activity by simply calling:
Intent i = getIntent();
Then you can make any changes to it (if needed) and send it again by calling:
startActivity(i);
or perhaps:
sendBroadcast(i);
depending on what you want to trigger.
I've ended up with launching Intent chooser.

Question regarding account creation and sync on Android

I've been reading the sample code from the dev docs on Android's site, specifically this:
http://developer.android.com/resources/samples/SampleSyncAdapter/src/com/example/android/samplesync/authenticator/AuthenticatorActivity.html
Which is the sole activity of the sample app. It refers to an intent in the onCreate method. I don't understand where this intent is coming from, or what it should contain if this is the only activity the app utilizes.
Log.i(TAG, "loading data from Intent");
final Intent intent = getIntent();
mUsername = intent.getStringExtra(PARAM_USERNAME);
mAuthtokenType = intent.getStringExtra(PARAM_AUTHTOKEN_TYPE);
mRequestNewAccount = mUsername == null;
mConfirmCredentials = intent.getBooleanExtra(PARAM_CONFIRM_CREDENTIALS, false);
That's the block of code working with the intent. Why would you have an intent for the only activity in the app? Is this app called in an unusual way? The Manifest does not include an intent filter for the activity... I guess I'm just a bit lost on this whole thing! If someone could set me straight that'd be great, thanks.
Why would you have an intent for the only activity in the app?
getIntent() gets you the intent that started this activity.
Is this app called in an unusual way?
I guess this activity is called programmatically from another app or activity, since it has been passed some extra data: getStringExtra() is used to extract some data from the intent that started it. putExtra.. and getExtra.. is a way to pass data between activities when they are started.
In that specific example, the intent is sent from the addAccount method in Authenticator.java. That method is called by the OS when you click the Add Account button in the Accounts & sync settings screen and choose your account type.

Categories

Resources