82 votes

Ouvrir la page dans l'application Twitter depuis une autre application - Android

J'ai cherché un moyen de lancer des application Twitter et ouvrir une page spécifiée à partir de mon application, sans webview. J'ai trouvé la solution pour Facebook ici: L'ouverture de facebook app sur la page de profil

J'ai besoin de quelque chose de similaire.

Je vous remercie beaucoup.

MODIFIER Je viens de trouver une solution:

try {
Intent intent = new Intent(Intent.ACTION_VIEW,
    Uri.parse("twitter://user?screen_name=[user_name]"));
startActivity(intent);

}catch (Exception e) {
    startActivity(new Intent(Intent.ACTION_VIEW,
         Uri.parse("https://twitter.com/#!/[user_name]"))); 
} 

Merci pour vos commentaires.

49voto

Harry Points 1143

Sur la base de la réponse fg.radigales, voici ce que j’avais utilisé pour lancer l’application si possible, mais revenons au navigateur autrement:

 Intent intent = null;
try {
    // get the Twitter app if possible
    this.getPackageManager().getPackageInfo("com.twitter.android", 0);
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USERID"));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch (Exception e) {
    // no Twitter app, revert to browser
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/PROFILENAME"));
}
this.startActivity(intent);
 

METTRE À JOUR

Ajouté intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); pour résoudre un problème où twitter s'ouvrait dans mon application plutôt que comme une nouvelle activité.

43voto

fg.radigales Points 186

Cela a fonctionné pour moi: twitter://user?user_id=id_num

Pour connaître l'ID: http://www.idfromuser.com/

4voto

igordcard Points 559

Ma réponse se base sur les réponses largement acceptées de fg.radigales et Harry. Si l'utilisateur a Twitter installé mais désactivé (par exemple, à l'aide de la mise en quarantaine de l'application), cette méthode ne fonctionnera pas. L'intention de l'application Twitter sera sélectionnée, mais elle ne pourra pas la traiter car elle est désactivée.

Au lieu de:

 getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));
 

Vous pouvez utiliser ce qui suit pour décider quoi faire:

 PackageInfo info = getPackageManager().getPackageInfo("com.twitter.android", 0);
if(info.applicationInfo.enabled)
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));
else
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/wrkoutapp"));
 

1voto

Akilan Points 1056

Essayez juste cet extrait de code. Cela vous aidera.

 //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) 
        {

        }
 

0voto

Jared Burrows Points 3932

Rendons cela simple et similaire à votre lien que vous avez posté:

 Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://twitter.com/#!/burrowsapps"));
this.startActivity(intent);
 

C'est une intention simple qui vise la page twitter de votre désir. Ceci utilise l'objet Uri exactement comme votre exemple. Faites-moi savoir si vous avez besoin de plus d'aide!

Cordialement,

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X