Unknown URL content://downloads/my_downloads Samsung triggered a bloodbath

Source: Internet
Author: User

This is a use of Downloadmanager in Samsung mobile phone triggered by the murders.

Definition:
Downloadmanager is a system service that has been added from API 9 to handle HTTP long connections, which we can use to request a URI to download to a target file. Downloadmanager will put the download in the background and will automatically try to download it once the HTTP connection fails, once the network is restored or the system restarts. We can get a Downloadmanager instance through Contex.getsystemservice (Download_service).

Scene:
Each app will use the download function, Google also considered the problem, so we provide downloadmanager this download for download management. Here are some of the major downloads I've used (the complete code is not available here, mainly talking about the problems encountered):

public static void installupdateapk (final string url, final string fileName, final string description) {
                New Thread () {public void run () {looper.prepare ();
                try {download (URL, fileName, description);
                    catch (Exception e) {apk_downloading = false;
                E.printstacktrace ();
            }} @SuppressWarnings ("Deprecation") @SuppressLint ({"Inlinedapi", "Newapi"}) private void Download (final string url, string fileName, string description) {F
                Ile d = new File (appconfig.download_dir);
                if (!d.exists ()) {d.mkdirs ();
                File F = new file (Appconfig.download_dir + fileName);
                if (f.exists ()) {f.delete ();
      } apk_downloading = true;          if (Mcontext = = null) {apk_downloading = false;
                Return
                } String downloadurl = URL;
                    if (DownloadURL = = null) {apk_downloading = false; Toast.maketext (Mcontext, "Download failed.")
                    ", Toast.length_long). Show ();
                Return

                    }//Check if support Download manager if (!isdownloadmanageravailable (Mcontext)) {
                    Intent Intent = new Intent ();
                    Intent.setaction ("Android.intent.action.VIEW");
                    Uri Content_url = Uri.parse (DownloadURL);
                    Intent.addflags (Intent.flag_activity_new_task);
                    Intent.setdata (Content_url);
                    Mcontext.startactivity (Intent);
                    Apk_downloading = false;
                Return
          Manager = (Downloadmanager) mcontext              . Getsystemservice (Context.download_service); Create download request Downloadmanager.request down = new Downloadmanager.request (uri.parse) (Do
                Wnloadurl));
                Set the type of network you are allowed to use, where mobile networks and WIFI are available//down.setallowednetworktypes (DownloadManager.Request.NETWORK_WIFI); Down.setallowednetworktypes (DownloadManager.Request.NETWORK_MOBILE |

                DownloadManager.Request.NETWORK_WIFI);
                Down.setmimetype ("application/vnd.android.package-archive");
                Down.setdescription (description);
                Prohibit notification, background download down.setshowrunningnotification (true);
                Do not display the download interface Down.setvisibleindownloadsui (TRUE);
                        Down.setdestinationinexternalpublicdir (appconfig.project_folder_name + "/download"),

                FileName); Put the download request into the queue MANAGER.ENqueue (down);
    }}.start (); }
public static Boolean isdownloadmanageravailable (context context) {
        try {
            if Build.VERSION.SDK_INT < Build.version_codes. Gingerbread) {return
                false;
            }
            return true;
        } catch (Exception e) {return
            false;
        }
    }

the problems encountered:
Now that you know it's from API 9, use the Isdownloadmanageravailable () method to make a decision to use the browser to download the lower version. In general, the majority of mobile phones are no problem after this judgment. But the actual use will always bring us surprises, one day a colleague with a Samsung mobile phone to tell me that the download is not used, but after debugging found the newspaper as the title of the error, and finally found that the original phone inside it installed a mobile defender, the system with the Download Manager is disabled (here I will * *).

Solution:
Fortunately, there are always more problems, in order to prevent other mobile phones will have similar problems, here is to determine whether the system's downloader can be used to choose the way to download. Through Context.getpackagemanager (). getapplicationenabledsetting ("Com.android.providers.downloads") The information we can get feedback on this method is: 0, 1, 2, 3, 4. By looking up the source details as follows:

In the Packagemanager class:

/** * Flag for {@link #setApplicationEnabledSetting (String, int, int)} * and {@link #setComponentEnabledSetting ( componentname, int, int)}: This * component or application was in its default-enabled state (as specified * in it
     s manifest).

    * * public static final int component_enabled_state_default = 0; /** * Flag for {@link #setApplicationEnabledSetting (String, int, int)} * and {@link #setComponentEnabledSetting ( componentname, int, int)}: This * component or application has been explictily enabled, regardless of * what it
     has specified in its manifest.

    * * public static final int component_enabled_state_enabled = 1; /** * Flag for {@link #setApplicationEnabledSetting (String, int, int)} * and {@link #setComponentEnabledSetting (  componentname, int, int)}: This * component or application has been explicitly disabled, regardless of * what it
     has specified in its manifest. * * public static final intcomponent_enabled_state_disabled = 2; /** * Flag for {@link #setApplicationEnabledSetting (String, int, int)} only:the * User has explicitly disabled  The application, regardless of what it has * specified in its manifest.  Because this are due to the user's request, * they may re-enable it if desired through the appropriate system UI. This * option currently <strong>cannot</strong> is used with * {@link #setComponentEnabledSetting (Co
     mponentname, int, int)}.

    * * public static final int component_enabled_state_disabled_user = 3; /** * Flag for {@link #setApplicationEnabledSetting (String, int, int)} Only:this * application should is consid  Ered, until the point where the user actually * wants to use it. This is means that it would not normally show the "user *" (such as in the launcher) but various the user I Nterface can * use {@link #GET_DISABLED_UNTIL_USED_COMPONENTS} to still the IT and AlloW * The user to select it (as for example an IME, device admin, etc).
     Such Code, * Once the user has selected the app, should at ' point ' also make it enabled. * This option currently <strong>can not</strong> to used with * {@link #setComponentEnabledSetting (compon
     entname, int, int)}. * * public static final int component_enabled_state_disabled_until_used = 4;

Here's a simple next 0: As long as the androidmanifest inside the open network access permissions are available.
1: The permissions are available regardless of whether or not the androidmanifest is turned on.
2: Whether or not the permissions are turned on in Androidmanifest are not available.
3: The user is forbidden to use the state.
4: The user does not choose to allow use before the state is not available.

From this we can see that only when the state is 0 or 1 of the time our app can normally use Downloadmanager, so we can make judgments, only need to change the Isdownloadmanageravailable method as follows:

public static Boolean isdownloadmanageravailable (context context) {try {if Build.VERSION.SDK_INT &L T Build.version_codes. Gingerbread | |
                                    Context.getpackagemanager (). Getapplicationenabledsetting ( "Com.android.providers.downloads") = = context. Getpackagemanager (). Component_enabled_state_disabled_user | |
                                    Context.getpackagemanager (). Getapplicationenabledsetting ( "Com.android.providers.downloads") = = context. Getpackagemanager (). component_enabled_state_disabled | |
                                    Context.getpackagemanager (). Getapplicationenabledsetting ( "Com.android.providers.downloads") = = context. Getpackagemanager ().

    component_enabled_state_disabled_until_used) {            return false;
        return true;
        catch (Exception e) {return false; }
    }

Good above is I in the use of Downloadmanager encountered problems, hereby do a record here. (-_-) !

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.