Ejemplo n.º 1
1
  public void download(String url, String fileName) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
      Uri uri = Uri.parse(url);

      DownloadManager.Request r = new DownloadManager.Request(uri);
      r.setAllowedNetworkTypes(Request.NETWORK_WIFI);
      r.setAllowedOverRoaming(false);

      // set mime type
      MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
      String mimeType =
          mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
      r.setMimeType(mimeType);

      // set in notification
      r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
      r.setVisibleInDownloadsUi(true);

      // sdcard
      if (TextUtils.isEmpty(fileName)) fileName = uri.getLastPathSegment();
      r.setDestinationInExternalFilesDir(mContext, File.separator, fileName);
      r.setTitle(fileName);

      // start download
      mDownloadId = mDM.enqueue(r);
    } else if (mListener != null) {
      SystemClock.sleep(1000);
      mListener.onDownloadComplete(null);
    }
  }
Ejemplo n.º 2
1
  /**
   * 下载一个文件
   *
   * @param dirType
   * @param subPath
   * @param url
   * @return
   */
  public long downLoadFile(String dirType, String subPath, final String url, boolean isExternal) {

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    /** 设置用于下载时的网络状态 */
    request.setAllowedNetworkTypes(
        DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    /** 设置通知栏是否可见 */
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
    /** 设置漫游状态下是否可以下载 */
    request.setAllowedOverRoaming(false);
    /** 如果我们希望下载的文件可以被系统的Downloads应用扫描到并管理, 我们需要调用Request对象的setVisibleInDownloadsUi方法,传递参数true. */
    request.setVisibleInDownloadsUi(true);
    /** 设置文件保存路径 */
    if (isExternal) {
      request.setDestinationInExternalPublicDir(dirType, subPath);
    } else {
      request.setDestinationInExternalFilesDir(context, dirType, subPath);
    }
    /** 将下载请求放入队列, return下载任务的ID */
    long downloadId = downloadManager.enqueue(request);

    Log.d(TAG, "下载Id:" + downloadId);

    // 在执行下载前注册内容监听者
    context
        .getContentResolver()
        .registerContentObserver(
            Uri.parse("content://downloads/"),
            true,
            DownloadManagerObserver.newInstance(
                new Handler(), context, DownloadUtilObserver.newInstance()));
    return downloadId;
  }
Ejemplo n.º 3
1
  /** 初始化下载器 * */
  private void initDownManager(Intent intent) {
    mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    mCompleteReceiver = new DownloadCompletedReceiver();
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String downName = intent.getStringExtra(DL_NAME);
    if (downName == null || downName.trim().equals("")) {
      throw new NullPointerException("downName---------下载名称不能为空");
    }
    String downUrl = intent.getStringExtra(DL_URL);
    if (downUrl == null || downUrl.trim().equals("")) {
      throw new NullPointerException("downUrl---------下载地址不能为空");
    }

    if (!prefs.getString(DL_NAME, "").equals(downName)) {
      // 设置下载地址
      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downUrl));
      // 设置允许使用的网络类型,这里是移动网络和wifi都可以
      request.setAllowedNetworkTypes(
          DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
      // 下载时,通知栏显示途中
      request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
      request.setVisibleInDownloadsUi(true);
      // 设置下载后文件存放的位置
      request.setDestinationInExternalFilesDir(
          this, Environment.DIRECTORY_DOWNLOADS, "baidumusic.apk");
      // 将下载请求放入队列
      long id = mDownloadManager.enqueue(request);
      // 保存id
      prefs.edit().putLong(DL_ID, id).apply();
      prefs.edit().putString(DL_NAME, downName).commit();
    } else {
      // 下载已经开始,检查状态
      queryDownloadStatus();
    }
    // 注册下载广播
    registerReceiver(mCompleteReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  }
Ejemplo n.º 4
1
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //    overridePendingTransition(R.anim.fadeinanimation, 0);
    setContentView(R.layout.activity_main);

    Backendless.setUrl(Defaults.SERVER_URL);
    Backendless.initApp(this, Defaults.APPLICATION_ID, Defaults.SECRET_KEY, Defaults.VERSION);

    mainScreen = findViewById(R.id.mainscreen);

    mlbbutton = findViewById(R.id.button1);
    nflbutton = findViewById(R.id.button2);
    nhlbutton = findViewById(R.id.button3);
    nbabutton = findViewById(R.id.button4);
    cfbbutton = findViewById(R.id.button5);
    cbbbutton = findViewById(R.id.button6);
    olybutton = findViewById(R.id.button7);

    //    String uri =
    // "https://api.backendless.com/B40D13D5-E84B-F009-FF57-3871FCA5AE00/v1/files/sportsreflinks.db";

    android.support.v7.app.ActionBar bar = getSupportActionBar();

    if (bar != null) {
      bar.setTitle("Sports Reference");
    }

    //  detector = new GestureDetector(this);

    prefs =
        this.getSharedPreferences("comapps.com.thenewsportsreference.app", Context.MODE_PRIVATE);

    boolean hasVisited = prefs.getBoolean("HAS_VISISTED_BEFORE", false);

    if (!hasVisited) {

      mlbbutton.setVisibility(View.INVISIBLE);
      nhlbutton.setVisibility(View.INVISIBLE);
      nbabutton.setVisibility(View.INVISIBLE);
      nflbutton.setVisibility(View.INVISIBLE);
      cfbbutton.setVisibility(View.INVISIBLE);
      cbbbutton.setVisibility(View.INVISIBLE);
      olybutton.setVisibility(View.INVISIBLE);

      /* //    Toast instructions = Toast.makeText(MainActivity.this,
      //            "Click for search.", Toast.LENGTH_LONG);
      //    instructions.setGravity(Gravity.CENTER, 0, -250);
      //    instructions.show();

      Toast instructions2 = Toast.makeText(MainActivity.this,
              "Click enter with no text in search box \n " +
                      "         to go to the main site.", Toast.LENGTH_LONG);
      instructions2.setGravity(Gravity.CENTER, 0, -200);
      instructions2.show();

      //   Toast instructions3 = Toast.makeText(MainActivity.this,
      //           "Touch above search box to close keyboard.", Toast.LENGTH_LONG);
      //   instructions3.setGravity(Gravity.CENTER, 0, -150);
      //   instructions3.show();*/

      prefs.edit().putBoolean("HAS_VISISTED_BEFORE", true).commit();

      downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadFileUrl));
      request.setTitle("Suggestions download.");
      request.setDescription("Suggestions database being downloaded...");

      request.setDestinationInExternalFilesDir(
          getApplicationContext(), "/dbase", "sportsreflinks.db");
      request.setNotificationVisibility(
          DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
      request.allowScanningByMediaScanner();
      // request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
      // "sportsreflinks.db");

      /*  if (!hasVisited) {

           request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
           request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE);

      } else {

          request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);

      }*/

      String filePath = prefs.getString("filepath", "");

      Log.i(TAG, "file path is " + filePath);

      fileExist = doesFileExist(filePath);
      Log.i(TAG, "file exists is " + fileExist.toString());

      if (fileExist == true) {

        deleteFileIfExists(filePath);
      }

      myDownloadReference = downloadManager.enqueue(request);

      intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);

      receiverDownloadComplete =
          new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
              long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

              if (myDownloadReference == reference) {
                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(reference);
                Cursor cursor = downloadManager.query(query);

                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                int status = cursor.getInt(columnIndex);
                int fileNameIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
                String saveFilePath = cursor.getString(fileNameIndex);

                fileExist = doesFileExist(saveFilePath);

                Log.i(TAG, "file exists download complete " + saveFilePath);

                prefs.edit().putString("filepath", saveFilePath).commit();

                int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
                int reason = cursor.getInt(columnReason);

                mlbbutton.setVisibility(View.VISIBLE);
                nhlbutton.setVisibility(View.VISIBLE);
                nbabutton.setVisibility(View.VISIBLE);
                nflbutton.setVisibility(View.VISIBLE);
                cfbbutton.setVisibility(View.VISIBLE);
                cbbbutton.setVisibility(View.VISIBLE);
                olybutton.setVisibility(View.VISIBLE);

                //     mainScreen.setEnabled(true);

                //     mainScreen.setClickable(true);

              }
            }
          };
    }

    mlbbutton.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            Intent intentbaseballsearch = new Intent();
            intentbaseballsearch.setClass(getApplicationContext(), MLBsearch.class);
            startActivity(intentbaseballsearch);
          }
        });

    nflbutton.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            Intent intentfootballsearch = new Intent();
            intentfootballsearch.setClass(getApplicationContext(), NFLsearch.class);
            startActivity(intentfootballsearch);
          }
        });

    nhlbutton.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            Intent intenthockeysearch = new Intent();
            intenthockeysearch.setClass(getApplicationContext(), NHLsearch.class);
            startActivity(intenthockeysearch);
          }
        });

    nbabutton.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            Intent intentbasketballsearch = new Intent();
            intentbasketballsearch.setClass(getApplicationContext(), NBAsearch.class);
            startActivity(intentbasketballsearch);
          }
        });

    cfbbutton.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            Intent intentcollegefootballsearch = new Intent();
            intentcollegefootballsearch.setClass(getApplicationContext(), CFBsearch.class);
            startActivity(intentcollegefootballsearch);
          }
        });

    cbbbutton.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            Intent intentcollegebasketballsearch = new Intent();
            intentcollegebasketballsearch.setClass(getApplicationContext(), CBBsearch.class);
            startActivity(intentcollegebasketballsearch);
          }
        });

    olybutton.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            Intent intentolympicssearch = new Intent();
            intentolympicssearch.setClass(getApplicationContext(), OLYsearch.class);
            startActivity(intentolympicssearch);
          }
        });
  }