So I'm downloading stuff and it gets put into the built in downloads app since thats how the download manager works. I just want to the user to click a button which opens the built in downloads app.
Heres my try:
btnDownloads.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
PackageManager pakMan=MainActivity.context.getPackageManager();
Log.d("bebr", "Making pak");
if(pakMan!=null){
Intent downloadsIntent=new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.android.downloads","com.android.downlods.Downloads"));
ResolveInfo resolved=pakMan.resolveActivity(downloadsIntent, PackageManager.MATCH_DEFAULT_ONLY);
Log.d("bebr","Resolving");
if(resolved!=null){
Log.d("bebr", "Starting");
startActivity(downloadsIntent);
}
}
}
});
Ok finally managed to get the solution:
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);
Use the DownloadManager.ACTION_VIEW_DOWNLOADS constant:
startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
You'd have to know the package name to launch the Downloads application. I am not certain that it is the same on every device either (though it may be). You can find it by watching the Logcat and launching it you should see some line in the log that has the package name in it.
However you can skip the downloads app completely and launch the package installer directly (which is what will happen when the user selects your apk in the downloads app)
just fill in the path to the file in the following snippet:
File appFile = new File("/path/to/your/file.apk");
Uri packageURI = Uri.parse("file:/"+ appFile.getAbsolutePath());
Intent installIntent = new Intent(Intent.ACTION_VIEW);
installIntent.setDataAndType(Uri.fromFile(appFile),"application/vnd.android.package-archive");
startActivity(installIntent);
Related
My app forwards users to a different app to perform a specific action (e.g. ACTION_SHARE, except that the apps that I forward users to do not implement an intent filter) Since they don't implement intent filters, I have a list of package names that support the action.
This part is working fine, like this:
for (String knownApp : knownApps) {
Intent intent = pm.getLaunchIntentForPackage(knownApp);
if (intent != null) {
ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
intentList.add(new LabeledIntent(intent, knownApp, resolveInfo.loadLabel(pm), resolveInfo.icon));
}
}
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
Intent openInChooser = Intent.createChooser(actionIntent, getString(R.string.perform_action_with));
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
If the user has none of the apps installed, I want to give the user a choice of apps to download to fulfill the action.
Currently that looks like this.
As you can see it's lacking both icon and name. For regular apps I use an intent chooser which needs LabledIntent, but on one hand, I can't get the name and icon from the playstore unless I scrape them (which is not allowed by google, besides LabledIntent requires a resourceId as the Icon, which I can't get for downloaded files.), on the other the intent chooser won't seem to display the intent unless the package name of the intent and LabeledIntent match. This does not work for URIs which I'm using to access the Play Store in the first place.
Now I'm looking for ideas on how to get the following code to display both the correct name and app icon, as well as forward to the correct page on the play store.
protected void showPlayStoreOptions(List<String> knownApps) {
Intent chooserIntent = new Intent();
Intent showIntent = Intent.createChooser(chooserIntent, "You need one of these Apps on Google Play..."); //googles brand guidelines state that "on Google Play" has to be used
List<Intent> list = new ArrayList<>();
for (String knownApp : knownApps) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + knownApp)); //normally you would try an uri with market:// first, catch the exception if no playstore is installed and then use this, but the intent chooser seems to automatically forward correctly.
list.add(intent);
//list.add(new LabeledIntent(intent, "https://play.google.com/store/apps/details?id=" + knownApp, "test name", R.drawable.icon_info));
//list.add(new LabeledIntent(intent, ""+Uri.parse("https://play.google.com/store/apps/details?id=" + knownApp), "test name", R.drawable.icon_info));
}
showIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, list.toArray(new Intent[list.size()]));
startActivity(showIntent);
}
So to sum up my questions.
How can I get a resource Id from a downloaded image file, or how can I use the downloaded image file with a LabledIntent.
(Extending LabledIntent does not work due to issues with parceling (and those methods are package private))
How can I display a LabledIntent in a choose intent with an URI?
I realize it's probably easier to write my own chooser, but I want to wrangle this into the default android system.
I have an android application. I just need to open youtube application home activity from my android application. Please note I do not want to open any specific video or channel. Just want to open application. I have gone through this and this but all are for opening any video or channel.
Please suggest me if anyone knows this.
Note: Application is running on TV and android version is 4.4
for opening any app form your device you have to know the package name of that app......
By firing an intent of that package name you can open an app form your app but if intent does not get a package name than no intent is their for handle that intent so take care of it...
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("any package name you want to open");
startActivity( LaunchIntent );
In your case it is youtube so package name is :
com.google.android.youtube
open app using package name youtube package name is com.google.android.youtube so you can use below intent code
try {
Intent LaunchIntent = packageManager.getLaunchIntentForPackage("com.google.android.youtube");
startActivity(LaunchIntent);
} catch (Exception e) {
e.printStackTrace();
}
just surround with try catch if in case package not found to get package name of any app use this Application on Google Play
same code work for Android TV
Actually the package name for youtube app in TV was not same as that in phones.
the package name for youtube app on TV is "com.google.android.youtube.googletv" so following code worked for me.
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.google.android.youtube.googletv");
startActivity( LaunchIntent );
This will work on a device but not the emulator
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));
Try this :
public static void openInstalledApp(Context context, String bundle_id) {
PackageManager pm = context.getPackageManager();
Intent appStartIntent = pm.getLaunchIntentForPackage(bundle_id);
if (null != appStartIntent) {
context.startActivity(appStartIntent);
}
}
And use it :
openInstalledApp(<your_context>,"com.google.android.youtube");
Where in second param you can pass any bundleID which is installed in your device and you wants to open it.
Our Android applications automatically check for updates every 5 minutes in the background and downloads the latest .apk file from our downloads server.
It then fires off an install using the below method:
public static void installDownloadedApplication(Context context) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, Constants.APPLICATION_CODE+".apk");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
context.startActivity(intent);
}
This prompts the end-user (using the standard Android OS application install prompt) to install or cancel the application apk.
If only one of our applications are in need of an update, the Android install prompt appears only once no matter how many times the above code in that one application runs.
The problem we are having is that if the user leaves his Android device for a long while and MULTIPLE of his applications need to auto update at the same time, this code is run every 5 minutes for each application, but now multiple Android install prompts appear for the second application that tries to install.
Example 1: only application X gets an update, the user leaves it for 15 mins, and only one install prompt for application X appears.
Example 2: both application X and Y get an update, the user leaves it for 15 mins, and 1 install prompts appear for application X but 3 install prompts appear for application Y
Any ideas what could be causing the problem in example 2?
Thanks
Your server tells you the latest APK. Compare this to the one in your download area. If you have already downloaded the latest version, you don't need to download it again.
Also, when you start the install via Intent, remember that by writing the version ID and date/time into a shared preferences. Don't try to install the same version again until X hours/days have passed.
I managed to get it working properly without the duplicates by having our background service call a custom activity:
public static void installDownloadedApplication(Context context) {
Intent intent = new Intent(context, InstallActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
Then our custom activity does what the background service used to do:
/**
* We use an activity to kick off the installer activity in order to avoid issues that arise when kicking off the apk installer from a background services
* for multiple applications at the same time.
* */
public class InstallActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, Constants.APPLICATION_CODE+".apk");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
this.startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
finish();
}
}
I want to download a PDF from a url and also want to trigger catch phrase if no PDF Viewer is detected.
Here's my code:
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(materialPdfUrl)));
} catch (ActivityNotFoundException e) {
openDialog(getString(R.string.error),
getString(R.string.no_pdf_reader));
}
Now the problem is that ActivityNotFoundException is never triggered because it always download the PDF even if there is no PDF Viewer around. How do you suggest I do this?
EDIT:
Here's my old code:
Intent pdfIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(materialPdfUrl));
pdfIntent.setType("application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(pdfIntent);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(materialPdfUrl)));
is starting an Implicit Intent and therefore will not throw ActivityNotFoundException.
If you read this http://developer.android.com/guide/components/intents-filters.html#ccases
Consider, for example, what the browser application does when the user follows a link on a web page. It first tries to display the data (as it could if the link was to an HTML page). If it can't display the data, it puts together an implicit intent with the scheme and data type and tries to start an activity that can do the job. If there are no takers, it asks the download manager to download the data. That puts it under the control of a content provider, so a potentially larger pool of activities (those with filters that just name a data type) can respond.
Therefore if no PDF viewers are found the Android Download Manager will attempt to download the file (rather than throw that exception).
If you want to view the pdf or be told you cannot view it (rather than download) then you will need to query the system manually using the PackageManager to find out if an application will respond to your intent rather than just firing and forgetting.
FYI ActivityNotFoundException will be thrown for Explicit Intent's something like:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.facebook","NewsFeedActivity.java"));
startActivity(intent);```
I would recommend using the PackageManager to detect if the system will handle a PDF intent for you.
PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW)
.setType("application/pdf");
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY;
if (list.size() > 0) {
// Happy days a PDF reader exists
startActivity(intent);
} else {
// No PDF reader, ask the user to download one first
// or just open it in their browser like this
intent = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("http://docs.google.com/gview?embedded=true&url=" + pdfUrl);
startActivity(intent);
}
See this blog post for more info on checking intents. The added benefit of this approach is that you can grey out/remove menu options before the app even tries to execute the Intent. Then you can explain to the user in a slightly more friendly way that they need to grab a PDF viewer app, or indeed apply some logic to fallback to a web based PDF viewer.
Having trialled this approach with Foxit Reader and Adobe Reader they both seem to have different behaviours. Foxit will not download the PDF for you, it will redirect you to the browser and download the file. Adobe will download the file for you then display it.
So to get round this difference once you have detected that a PDF viewer is available then you will probably want to download the PDF to the SD card, for example the downloads folder. This is probably best achieved in an AsyncTask, or you might be able to use the DownloadManager. Then open the local file Uri in the preferred reader. This should get round the difference in behaviour. Maybe open a ticket with Foxit to bring it into line with Adobe? ;)
The function startActivity() you use is not on the condition that the PDF reader is not exist, it only download the PDF from the URL, and if there are PDF Readers then it will offer a selector, just as the function of clicking on a PDF file.
you may try this code. This may helpful to you.
Intent intent = new Intent();
intent.setClassName("com.adobe.reader", "com.adobe.reader.AdobeReader");
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
try {
startActivity(intent);
} catch (Exception e) {
AlertDialog.Builder builder = new AlertDialog.Builder(
getApplicationContext());
builder.setTitle("No Application Found");
builder.setMessage("Download application from Android Market?");
builder.setPositiveButton(
"Yes, Please",
new DialogInterface.OnClickListener() {
#Override
public void onClick(
DialogInterface dialog,
int which) {
Intent marketIntent = new Intent(
Intent.ACTION_VIEW);
marketIntent.setData(Uri
.parse("market://details?id=com.adobe.reader"));
mProgressDialog.dismiss();
startActivity(marketIntent);
}
});
builder.setNegativeButton("No, Thanks",
null);
builder.create().show();
}
That is because your device or emulator does not have an application capable of viewing a local PDF file.
Whenever you start an intent, you should have the native app installed on the emulator to handle that intent. Ex. If you invoke an intent with Maps as the action, you would have to use the Google API's based emulator. By default, android emulator does not have a PDF reader. You could test this on a device with a PDF reader and it should work fine.
use startActivityForResult(..).
See the link here.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivityForResult(intent, REQUEST_CODE);
You will get the result in onActivityResult(..) of your activity.
How could I write a code which can tell me that android market is installed on your android phone?
There are two ways. You can use the already mentioned getPackageManager() and getApplicationInfo() (if the package is not found, a PacketManager.NameNotFoundException will be thrown - see here). Android Market's package name is com.android.vending.
However, you can also create a dummy intent for searching the market and check how it is handled. If the resulting list has at least one entry, you can be sure that Android Market is installed:
Intent market = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=dummy"));
PackageManager manager = getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(mmarket, 0);
Here's what I did (assumes browser exists):
Intent market = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse("market://details?id=com.example.app"));
Intent website = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse("http://play.google.com/store/apps/details?id=com.example.app"));
try {
startActivity(market);
} catch (ActivityNotFoundException e) {
startActivity(website);
}