Según mis pruebas, no pude encontrar una buena manera de hacerlo, y en su lugar recurrí a una solución que no es necesariamente la "mejor práctica". Funciona solo con la aplicación oficial de Twitter y no con los demás. Esta solución fallará si la aplicación oficial cambia su API interna. Por lo tanto, utilice esta solución con precaución y conozca sus limitaciones.
Este código no está escrito de una buena manera, pero funciona. Mi consejo es cambiarlo para que no use tantos recursos.
El código comprueba si la aplicación de Twitter está instalada. Si es así, se lanza la aplicación de Twitter; de lo contrario, se inicia el navegador.
Twitter tiene el nombre de twitter (también llamado screen_name) y la identificación de twitter: no son lo mismo.
//Checking If the app is installed, according to the package name
Intent intent = new Intent();
intent.setType("text/plain");
intent.setAction(Intent.ACTION_SEND);
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : list)
{
String packageName = resolveInfo.activityInfo.packageName;
//In case that the app is installed, lunch it.
if (packageName != null && packageName.equals("com.twitter.android"))
{
try
{
String formattedTwitterAddress = "twitter://user/" ;
Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress));
long twitterId = <Here is the place for the twitter id>
browseTwitter.putExtra("user_id", twitterId);
startActivity(browseTwitter);
return;
}
catch (Exception e)
{
}
}
}
//If it gets here it means that the twitter app is not installed. Therefor, lunch the browser.
try
{
String twitterName = <Put the twitter name here>
String formattedTwitterAddress = "http://twitter.com/" + twitterName;
Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress));
startActivity(browseTwitter);
}
catch (Exception e)
{
}
ok..so ¿Cuál es el contexto aquí? – AutoMEta
No ... tweetIntetnt no funciona ... Entiendo esto cuando empiezo esto. complete acción usando enviar por Bluetooth, Dropbox, Gmail – AutoMEta
Consulte la segunda fuente para obtener más información sobre el tweetIntent –