简体   繁体   中英

check to see if twitter app is present on user's device

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.

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 .

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

boolean twitterInstalled = false;

    try{
        ApplicationInfo info = getPackageManager().
                getApplicationInfo("com.twitter.android", 0 );
        twitterInstalled = true;
    } catch( PackageManager.NameNotFoundException e ){
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM