Skip to content

Instantly share code, notes, and snippets.

@luiscoms
Created November 11, 2015 17:48
Show Gist options
  • Save luiscoms/c1d09655d49a602f4ad9 to your computer and use it in GitHub Desktop.
Save luiscoms/c1d09655d49a602f4ad9 to your computer and use it in GitHub Desktop.
Call another application if is installed
package com.example.app.fragments;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.zerohora.app.R;
public class CallApplicationFragment extends Fragment {
private static final String APPLICATION_PACKAGE = "br.com.luiscoms.gravityworld";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean installed = appInstalledOrNot(APPLICATION_PACKAGE);
if (installed) {
Intent LaunchIntent = getActivity()
.getPackageManager()
.getLaunchIntentForPackage(APPLICATION_PACKAGE);
startActivity(LaunchIntent);
// getActivity().finish(); finish the actual application
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_digital_news, container, false);
if (v != null) {
(v.findViewById(R.id.btn_gotoApplication)).setOnClickListener(gotoApplication);
}
return v;
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getActivity().getPackageManager();
boolean app_installed;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
private View.OnClickListener gotoApplication = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(String.format("market://details?id=%s", APPLICATION_PACKAGE)));
startActivity(intent);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment