I want to update an apk through code. However I need to establish a mechanism that checks when that application has updated. I need to somehow get a message back from the intent
Context ctx = getApplicationCOntext();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/mnt/sdcard/Download/update.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
This is the default mechanism to install an application. How can I check that if the intent has finished?
Use startActivityForResult() if you want data to be return from the called activity. For more details, read: Getting a Result from an Activity How to manage startActivityForResult on Android?
Related
I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();
** Code used in First/Parent Activity of Application A
Intent LaunchIntent = new Intent();
LaunchIntent.putExtra("ComingFrom", "RentsComments");
LaunchIntent.setClassName("packageNameOfApplicationB", "activityName");
startActivityForResult(LaunchIntent, requestCode);
**Code used in SecondActivity:**
Intent launchIntent = new Intent();
launchIntent.putExtra("transaction_data_id", 123456);
launchIntent.putExtra("transaction_data_authorised_amount", 10.0);
launchIntent.putExtra("transaction_data_status", "finished");
// launchIntent.setClassName("packageName", "fullQualifiedActivityNameInPackage");
setResult(2520,launchIntent);
finish();
I couldn't understand why the onActivityResult in ParentActivity is not firing once after calling it
You cannot use startActivityForResult() when the target has the singleTask launch mode. Or rather, you can, but you will get an immediate RESULT_CANCELLED, not the actual result.
From the documentation:
Note that this method should only be used with Intent protocols that
are defined to return a result. (...) For example, if the activity you
are launching uses the singleTask launch mode, it will not run in your
task and thus you will immediately receive a cancel result.
Unfortunately, there is no workaround for this (AFAIK).
I'm able to launch the Call Log activity using intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(android.provider.CallLog.Calls.CONTENT_URI);
startActivity(intent);
But on selecting any Log (incoming, Outgoing or missed ) from Call Log activity it will initiate a call.
But according to my requirement i want to use above intent using action Intent.ACTION_PICK and startActivityForResult(intent).
please share any idea to do the same.
I am creating an application where I need to call another application that is already installed in the device on button click.
I have done some research on it and I understand that I will need to call an intent for the same. What I dont understand is I do not have a class name for the application I want to call. For example, if I want to call the device's gallery from my application on button click, how do I do that?
Uri uri = Uri.parse("file:///sdcard/");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
intent.setType("application/pdf");
startActivity(intent);
return true;
Thanks guys. I have tried this code but it said document could not be opened.
You will need to call Implicit Intents
From the documentation:
Implicit Intents have not specified a component; instead, they must
include enough information for the system to determine which of the
available components is best to run for that intent.
These intents can be triggered providing any action, type or category information
For example you want to open browser Activity and you don't know the Activity class name you will use something like this:
Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(in);
Another example: you don't know the Gallery Activity class, you will call it using Implicit Intent like this:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
Here the call
Enjoy coding....
Intent res = new Intent();
String mPackage = "com.ReachOut";
String mClass = ".splash1";
res.setComponent(new ComponentName(mPackage,mPackage+mClass));
startActivity(res);
If the exact application is less important, and you just need something that will display your content, you can omit the component name entirely, too. Just set the action, data, and (optionally) type on the intent and let the OS do the work.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(/*your data uri*/);
intent.setType(/*your data's MIME type*/);
startActivity(intent);
This intent will be handled by some app that has registered for intents with the view action and the appropriate MIME type. This is an implicit intent, like Adil mentioned.
Is it possible to start an Activity from a Service? If yes, how can we achieve this?
android.app.Service is descendant of android.app.Context so you can use startActivity directly. However since you start this outside any activity you need to set FLAG_ACTIVITY_NEW_TASK flag on the intent.
For example:
Intent i = new Intent();
i.setClass(this, MyActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
where this is your service.
Even if the framework allows you to start an Activity from a Service, it's probably not a proper solution. The reason is that the Service task may or may not be the focus of the user at the time the Service wishes to interact with the user. Interrupting what the user is currently doing is considered bad design form, especially from something that is supposed to be operating in the background.
Therefore, you should consider using a Notification with Notification Service, which carries a PendingIntent to launch the desired Activity when the user decides it is time to investigate. Think of it as delayed gratification.
I had a problem starting an activity from a service, it was due to the missing FLAG_ACTIVITY_NEW_TASK intent flag.
This surely will solve your problem
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(this, TaxiPlexer.class);
intent.setComponent(cn);
startActivity(intent);