Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Inside my app, I want to detect if an app like VLC is installed.
If it does, and when user clicks button, my app will proceed to play a video but if does not, the app will direct user to App store to download the app.
Is there a way to detect if an particular app is installed or not?
Try this:
PackageManager packageManager = getPackageManager();
List activities = packageManager.queryIntentActivities(***REPLACE_WITH_YOUR_INTENT***,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activities.size() > 0;
From the Android Docs:
Although the Android platform guarantees that certain intents will
resolve to one of the built-in apps (such as the Phone, Email, or
Calendar app), you should always include a verification step before
invoking an intent.
To verify there is an activity available that can respond to the
intent, call queryIntentActivities() to get a list of activities
capable of handling your Intent. If the returned List is not empty,
you can safely use the intent.
Note: You should perform this check when your activity first starts in
case you need to disable the feature that uses the intent before the
user attempts to use it. If you know of a specific app that can handle
the intent, you can also provide a link for the user to download the
app (see how to link to your product on Google Play).
So, you could use that like this:
Intent vlcIntent = new Intent(Intent.ACTION_VIEW);
vlcIntent.setPackage("org.videolan.vlc");
PackageManager packageManager = getPackageManager();
List activities = packageManager.queryIntentActivities(vlcIntent,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activities.size() > 0;
if (isIntentSafe){
startActivity(vlcIntent);
} else {
Intent marketIntent = new Intent(Intent.ACTION_VIEW);
marketIntent.setData(Uri.parse("market://details?id=org.videolan.vlc"));
startActivity(marketIntent);
}
Also, another link that might be useful(VideoLan Wiki):
Android Video Player Intents
It says:
For a simple media playback:
int vlcRequestCode = 42; Uri uri =
Uri.parse("file:///storage/emulated/0/Movies/KUNG FURY Official
Movie.mp4"); Intent vlcIntent = new Intent(Intent.ACTION_VIEW);
vlcIntent.setPackage("org.videolan.vlc");
vlcIntent.setDataAndTypeAndNormalize(uri, "video/*");
vlcIntent.putExtra("title", "Kung Fury");
vlcIntent.putExtra("from_start", false);
vlcIntent.putExtra("position", 90000l);
vlcIntent.putExtra("subtitles_location",
"/sdcard/Movies/Fifty-Fifty.srt"); startActivityForResult(vlcIntent,
vlcRequestCode);
If you specifically want to start VideoPlayerActivity, you can set
vlcIntent.setComponent(new ComponentName("org.videolan.vlc",
"org.videolan.vlc.gui.video.VideoPlayerActivity"));
You could also put the code into a boolean code block and pass in your intent, that way you could reuse the code. Like this:
private boolean checkIntent(Intent intent) {
PackageManager packageManager = getPackageManager();
List activities = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activities.size() > 0;
return isIntentSafe;
}
You will need to check to see if the package for VLC is installed. If it is not installed then open the play store showing the vlc install page.
This is the source:
String appPackageName = "org.videolan.vlc";
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
The source below will check to see if there is anything that can play video and if not, then it will prompt the user to install VLC. This is a more complete example that might help other users. It checks to see if anything locally installed can play the file and then if not, checks for vlc, and if VLC is not installed, then it will take the user to the Play Store.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
intent.setDataAndType(Uri.fromFile(file), mimetype);
PackageManager manager = context.getPackageManager();
List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent, 0);
if (resolveInfoList.size() > 0) {
context.startActivity(intent);
} else {
FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
ClassChatRecyclerAdapter.InstallVlcDialogFragment.newInstance(extension).show(fragmentManager, "INSTALL_VLC");
}
This is the dialog that will check for VLC and if it doesn't find it, then it will take the user to the play store to install it. You can take use the try/catch part of the source below to specifically solve your problem.
public static class InstallVlcDialogFragment extends DialogFragment {
public static ClassChatRecyclerAdapter.InstallVlcDialogFragment newInstance(String mediaType) {
ClassChatRecyclerAdapter.InstallVlcDialogFragment frag = new ClassChatRecyclerAdapter.InstallVlcDialogFragment();
Bundle args = new Bundle();
args.putString("mediaType", mediaType);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String mediaType = getArguments().getString("mediaType");
return new AlertDialog.Builder(getActivity(), R.style.AppDialogTheme)
.setTitle("Unable to find video player for " +mediaType + " files.")
.setMessage("Install VLC Player to play file?")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String appPackageName = "org.videolan.vlc";
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
dismiss();
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dismiss();
}
}
)
.create();
}
}
Related
I have an Unity app, that has Android plugin that can launch other applications that installed on my smartphone. Here My Java class:
public class LaunchOtherApp extends Activity
{
public static Activity mainActivity;
protected static final String LOGTAG = "MyApp";
private Activity currentActivity;
private Intent i;
private static final LaunchOtherApp ourInstance = new LaunchOtherApp();
public static LaunchOtherApp getInstance() {
return ourInstance;
}
public LaunchOtherApp(){
Log.i(LOGTAG,"Created LaunchOtherApp");
}
public void Launch( final String pack)
{
mainActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
Intent intent = new Intent();
intent.setPackage(pack);
currentActivity = UnityPlayer.currentActivity;
Context context = currentActivity.getApplicationContext();
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));
if (resolveInfos.size() > 0)
{
try
{
ResolveInfo launchable = resolveInfos.get(0);
ActivityInfo activity = launchable.activityInfo;
ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
i = new Intent(Intent.ACTION_MAIN);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
context.startActivity(i);
}
catch (SecurityException e)
{
intent = getPackageManager().getLaunchIntentForPackage(pack);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
}
}
}
});
}
#Override
public void onBackPressed() {
finish();
}
Launch - it is method that starting when I click on button in my Unity app... pack variable - it is variable that my Java method receive, and in this variable instantiate Application id. For Example I launched Youtube from my app, and when I pressing Back on my phone, I want to close Youtube, and back to my Unity app... I think I must use onBackPressed method that start when I press Back, but what I must write in this method?
finish(); doesn't help me(( Please, help... Thank you in advance!
Actually, YouTube app is a separate app (might be installed) and you cannot modify that app's behavior. On your button press, Launch method is called and assume YouTube app has started. With the intent:
If you use Intent.FLAG_ACTIVITY_NEW_TASK
YouTube app will start in a new task than yours and pressing back will not return to your app.
If you do not use Intent.FLAG_ACTIVITY_NEW_TASK
The new intent (here YouTube) will be launched in the task of calling activity (your app/game) by default and calling activity will go background. So, pressing back will again return to your app/game as you desired. Because back button press always pops from the backstack of the current task.
So, in the try block of your Launch method, removing the following line should help.
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
For more information about task and back stack, here is the official doc.
Edit
I missed that you have used application context. But, you have to launch from your calling activity context, otherwise you must use this flag. So, I have modified your Launch method and put it here, just replace your Launch method with following:
/* Method to launch an app with its package name */
public void Launch( final String pack)
{
mainActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
// Using activity context instead of application context
currentActivity = UnityPlayer.currentActivity;
Context context = currentActivity;
PackageManager pm = context.getPackageManager();
// Similar code to launch from package name
Intent intent = pm.getLaunchIntentForPackage(pack);
if (intent == null) {
// The activity cannot be found or the package name cannot be recognized
// Show some message or do something
Toast.makeText(context, "Cannot launch...", Toast.LENGTH_SHORT).show();
} else {
context.startActivity(intent);
}
}
});
}
currently i am using this but(to open map location on image click but some device having GoogleMap cant open the intent) the issue is even device has apps to handle the map intent it wont execute that part
any better way to do this?
like option to select from available apps and no apps to handle that then go for browser
private void openMap()
{
String Packagename ="com.google.android.apps.maps";
if (appInstalledOrNot(Packagename))
{
Uri uri = Uri.parse("geo:0,0?q=V.V.P.+Engineering+College+Kalavad+Road+Virda+Vajadi,+Rajkot");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
}else
{
try {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://goo.gl/maps/ETVvFWw453CoeBHs9"));
startActivity(myIntent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
Toast.makeText(getContext(), "Map application not found", Toast.LENGTH_SHORT).show();
}
}
I have some links in my webview that are market:// links. When my users tap on them, it gives them a page cannot be found error.
How can I allow all links that begin with market:// to automatically open the Google play store when they are tapped? I tried:
final Intent intent = new Intent("android.intent.action.VIEW");
intent.setData(Uri.parse("market://details?id="));
startActivity(intent);
}
but that didn't seem to do anything. I am pretty new to this so any help would be appreciated. Also, FYI, I cannot change the market:// links to play.google.com myself. They are from my advertiser.
Is there anyway I can include it in this code:
public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString) {
if (DEBUG)
Log.e("shouldOverride", paramString);
if (Uri.parse(paramString).getHost()!=null && (!Uri.parse(paramString).getHost().equals("market.android.com")) && (!paramString.contains("facebook.com")) && (!Uri.parse(paramString).getHost().contains("twitter.com")) && (!Uri.parse(paramString).getHost().equals("play.google.com"))
&& (!Uri.parse(paramString).getHost().contains("bit.ly")) && (!Uri.parse(paramString).getHost().contains("plus.google.com")) && (!Uri.parse(paramString).getHost().contains("youtube.com"))){
if(isAppOrGamePage(paramString)){
final Intent intent = new Intent(MainActivity.this, PageActivity.class);
intent.putExtra("app_url", paramString);
startActivity(intent);
} else
return false;
} else {
final Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(paramString));
startActivity(intent);
}
return true;
}
}
You can decide what to do by looking the scheme of the url, if Google Play Store app is installed you can open the detail page in Play Store app, else you can show Google Play web page of the application
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getScheme().equals("market")) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
Activity host = (Activity) view.getContext();
host.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
// Google Play app is not installed, you may want to open the app store link
Uri uri = Uri.parse(url);
view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
return false;
}
}
return false;
}
});
you can use this code like this also if its help you:
// It will not work in android simulator as it does not have Google Play Store
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+APP_ID)));
if (url.startsWith("market://")||url.startsWith("vnd:youtube")||url.startsWith("tel:")||url.startsWith("mailto:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
In my Android app, I have a button that when clicked, launches the external application of my choice to play a video (I gather that this is called an "implicit intent"). Here is the relevant Java code from my onCreate method.
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener
(
new Button.OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(i);
}
}
);
I expected this to work, since I've followed tutorials and the Android developers documentation pretty closely, but when I test my app in the AVD, instead of prompting a menu of external applications where I can view my video, the app crashes.
What is causing my app to crash?
Change your onClick method to below code. You should give the option to choose the external player.
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(Intent.createChooser(intent, "Complete action using"));
}
Change your code to add this check:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
// Check there is an activity that can handle this intent
if (i.resolveActivity(getPackageManager()) == null) {
// TODO No activity available. Do something else.
} else {
startActivity(i);
}
I want to write small Android app to send the message through Viber
to people whom are listed in my contact list. But I could not find
any sample code to do this task.
If you know how to do this task.
Please teach me.
Vonbk
If viber application is installed in your device, You can call an intent to share the text.
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = context.getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase(
Locale.getDefault()).contains("com.viber.voip")
|| info.activityInfo.name.toLowerCase(
Locale.getDefault()).contains("com.viber.voip")) {
share.putExtra(Intent.EXTRA_TEXT, "Your text to share");
share.setPackage(info.activityInfo.packageName);
found = true;
context.startActivity(Intent.createChooser(share, "Select"));
break;
}
}
if (!found) {
displayToast(context, "Install viber android application");
Uri marketUri = Uri.parse("market://details?id="
+ "com.viber.voip");
Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
context.startActivity(marketIntent);
}
}
I am not sure it will work. But it will worth a shot.
You can also share with the plain intent which asks the user to select and share :
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text that will be shared.</p>"));
startActivity(Intent.createChooser(sharingIntent,"Share using"));