Created
April 29, 2011 12:41
-
-
Save esaounkine/948236 to your computer and use it in GitHub Desktop.
Intent to start navigation activity - payload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final int defaultFlag = PackageManager.MATCH_DEFAULT_ONLY; | |
Intent[] explicitIntents; | |
//see an example here http://stackoverflow.com/questions/2662531/launching-google-maps-directions-via-an-intent-on-android | |
private Intent[] getExplicitIntents() { | |
if(explicitIntents == null) { | |
PackageManager currentPM = getPackageManager(); | |
explicitIntents = new Intent[]{ | |
new Intent("android.intent.action.navigon.START_PUBLIC"), //navigon with public intent | |
currentPM.getLaunchIntentForPackage("com.navigon.navigator"), //navigon without public intent | |
currentPM.getLaunchIntentForPackage("hr.mireo.dp"), //ginius driver dont panic | |
currentPM.getLaunchIntentForPackage("com.ndrive.android"), //ndrive | |
currentPM.getLaunchIntentForPackage("com.sygic.aura"), //aura | |
currentPM.getLaunchIntentForPackage("org.microemu.android.se.appello.lp.Lightpilot") // wisepilot | |
}; | |
} | |
return explicitIntents; | |
} | |
/** | |
* Following the suggestion at http://stackoverflow.com/questions/5801684/intent-to-start-a-navigation-activity/5809637#5809637 | |
* Will query up the system for desired applications and then let the user decide which one they would like to launch. | |
*/ | |
protected void showNavigation() { | |
//explicit activities (known apps) | |
//build the list and show it in a dialog | |
int titleColor = Color.rgb(28, 97, 211); | |
int headerColor = Color.rgb(98, 151, 251); | |
HashMap<String, ArrayList<TableRow>> rows = new HashMap<String, ArrayList<TableRow>>(); | |
ArrayList<TableRow> tableRows = new ArrayList<TableRow>(); | |
TableRow row = new TableRow(this); | |
row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); | |
row.setGravity(Gravity.CENTER); | |
row.setBackgroundColor(headerColor); | |
TextView recyclableTextView = new TextView(this); | |
recyclableTextView.setText("Explicitly known"); | |
recyclableTextView.setTextColor(Color.BLACK); | |
recyclableTextView.setTextSize(20); | |
int screenWidth = getResources().getDisplayMetrics().widthPixels; | |
recyclableTextView.setWidth(100 * screenWidth / 100); //take up 100% of the width | |
recyclableTextView.setPadding(5, 5, 5, 5); | |
row.addView(recyclableTextView); | |
tableRows.add(row); | |
PackageManager currentPM = getPackageManager(); | |
for(int i = 0; i < getExplicitIntents().length; i++) { | |
Intent navigationAppIntent = explicitIntents[i]; | |
try { | |
for(ResolveInfo explicitActivityInfo : currentPM.queryIntentActivities(navigationAppIntent, defaultFlag)) { | |
try { | |
row = new TableRow(this); | |
row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); | |
row.setGravity(Gravity.CENTER); | |
recyclableTextView = new TextView(this); | |
recyclableTextView.setText(explicitActivityInfo.loadLabel(currentPM) + " (" + explicitActivityInfo.activityInfo.applicationInfo.packageName + ")"); | |
recyclableTextView.setTypeface(Typeface.DEFAULT_BOLD); | |
recyclableTextView.setTextColor(Color.BLACK); | |
recyclableTextView.setTextSize(20); | |
recyclableTextView.setWidth(100 * screenWidth / 100); | |
recyclableTextView.setPadding(5, 5, 5, 5); | |
row.addView(recyclableTextView); | |
row.setContentDescription("" + i); //this descriptor will be used in the following listener to find out which row was selected | |
//set onclick listener to each row (it already has a descriptor pointing the intent index it represents from the array) | |
row.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String desc = "" + v.getContentDescription(); | |
if(desc != null) { | |
try { | |
startActivity(getExplicitIntents()[Integer.parseInt(desc)]); | |
} | |
catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}); | |
row.setMinimumHeight(100); | |
tableRows.add(row); | |
} | |
catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
//add implicit apps option | |
row = new TableRow(this); | |
row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); | |
row.setGravity(Gravity.CENTER); | |
row.addView(makePoiTableRowWithText("Find Implicitly", true, 100)); | |
row.setMinimumHeight(150); | |
//add a listener to this view to let the OS find matching applications | |
row.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent implicitIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q="); //NOTE: google navigation can't be launched without destination | |
startActivity(implicitIntent); | |
} | |
}); | |
tableRows.add(row); | |
rows.put("Compatible apps", tableRows); | |
showTableDialog(rows, "Launch GPS Navigator", titleColor, R.drawable.menu_gps); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment