Determine if a apk file is a home application

Source: Internet
Author: User
Tags int size

How to determine if a apk file is a home application

Imported Packages:

Import Android.content.Context;
Import android.content.Intent;
Import Android.content.pm.PackageParser;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.support.design.widget.FloatingActionButton;
Import Android.support.design.widget.Snackbar;
Import android.support.v7.app.AppCompatActivity;
Import Android.support.v7.widget.Toolbar;
Import Android.util.DisplayMetrics;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Java.io.File;
Import java.util.ArrayList;

Decision function:

Boolean Ishomeapp (context context, String Dexpath) {Boolean ishomecategory = false;

        Dexpath = Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/testmarket.apk";
        File Dexfile = new file (Dexpath);
        LOG.I ("Findbug", "Dexpath =" + Dexpath);
        if (!dexfile.exists ()) {//log.i ("Findbug", "file can ' t find");
        }/* Packagemanager Packagemanager = Context.getpackagemanager ();

        PackageInfo PackageInfo = Packagemanager.getpackagearchiveinfo (Dexpath, packagemanager.get_activities);
            if (PackageInfo = = null) {LOG.I ("Findbug", "pkg = = null.");
        return false;

            for (Activityinfo a:packageinfo.activities) {log.i ("Findbug", a.name);
        PackageInfo.
        } * * Displaymetrics displaysmetrics = new Displaymetrics ();

        Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Displaysmetrics); Packageparser PackAgeparser = new Packageparser (Dexpath);

        Packageparser.package dexpackage = Packageparser.parsepackage (Dexfile, Dexpath, displaysmetrics, 0);
        final int size = DexPackage.activities.size ();
            found:for (int i = 0; i < size; i++) {Packageparser.activity activity = dexPackage.activities.get (i);

            arraylist<packageparser.activityintentinfo> intents = activity.intents;
            Final int sizeintent = Intents.size ();
                for (int j = 0; j < sizeintent; ++j) {Packageparser.activityintentinfo intentinfo = Intents.get (j);
                Final int sizeactions = Intentinfo.countactions ();

                Final int sizecategories = Intentinfo.countcategories ();
                        for (int k = 0; k < sizeactions; ++k) {for (int m = 0; m < sizecategories; ++m) { if (Intentinfo.getcategory (m). Equals (Intent.category_home)) {Ishomecategory = true;
                        Break found;
    }}} return ishomecategory; }

Please indicate the error, reprint, pls mark the source, thank you



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.