6

In my project i want to connect my app with twitter. It will first check if twitter app is present on user's device if yes it will take credential from there only else this button will be disabled.

suggest me.

Avinash Kumar Pankaj
  • 1,700
  • 2
  • 18
  • 27

3 Answers3

8

You can use this to check if the official Twitter application is installed:

PackageManager pkManager = activity.getPackageManager();
try {
    PackageInfo pkgInfo = pkManager.getPackageInfo("com.twitter.android", 0);
    String getPkgInfo = pkgInfo.toString();

    if (getPkgInfo.equals("com.twitter.android"))   {
        // APP NOT INSTALLED
    }
} catch (NameNotFoundException e) {
    e.printStackTrace();

    // APP NOT INSTALLED

}

However, even if it is installed, you will not be able to pull out any credentials from it to use within your own app. You will need to the Twitter4J library to manage user authentication within your own app. Pulling out data from the app, if it were installed, is just not an option.

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • ohk. then can i start that app for authentication?? – Avinash Kumar Pankaj May 16 '13 at 10:48
  • @AvinashKumarPankaj: Actually, the authentication will be done in a browser. It could be a `WebView` in your app or you could let the user choose a browser of his choice (_from among those installed on his device_) to complete the authentication process. But unlike the Facebook SSO, the Twitter app is not involved in this process. – Siddharth Lele May 16 '13 at 10:55
  • Very helpful response, my use of this code is in the link below if useful to anyone: https://github.com/lararufflecoles/KittyCatImageGenerator/blob/master/app/src/main/java/es/rufflecol/lara/kittycatimagegenerator/MainActivity.java – Lara Ruffle Coles Mar 03 '16 at 14:10
  • @LaraRuffleColes: Thank you. Glad to have been of help. I did visit the GitHub link and would want to make a quick suggestion. Don't include, what I am assuming are, the real Twitter API Key and Secret in it. ;-) – Siddharth Lele Mar 03 '16 at 14:32
  • I've thought about that thanks, decided it wasn't a big deal for my wee app. :-). – Lara Ruffle Coles Mar 03 '16 at 19:09
3
try{
    ApplicationInfo info = getPackageManager().
            getApplicationInfo("com.twitter.android", 0 );
    return true;
} catch( PackageManager.NameNotFoundException e ){
    return false;
}

this will check if the official app for Twitter is installed or not

Syn3sthete
  • 4,151
  • 3
  • 23
  • 41
1
boolean twitterInstalled = false;

    try{
        ApplicationInfo info = getPackageManager().
                getApplicationInfo("com.twitter.android", 0 );
        twitterInstalled = true;
    } catch( PackageManager.NameNotFoundException e ){
    }
Alexis C.
  • 91,686
  • 21
  • 171
  • 177