Guest User

Untitled

a guest
May 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. package com.bgstation0.android.sample.downloadmanager.query_;
  2.  
  3. import android.app.Activity;
  4. import android.app.DownloadManager;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.database.Cursor;
  9. import android.widget.Toast;
  10.  
  11. public class DownloadReceiver extends BroadcastReceiver {
  12.  
  13. // メンバフィールドの定義
  14. private MainActivity mainActivity = null; // MainActivity型mainActivityをnullにセット.
  15.  
  16. // コンストラクタ
  17. public DownloadReceiver(MainActivity mainActivity){
  18. this.mainActivity = mainActivity; // mainActivityをthis.mainActivityに格納.
  19. }
  20.  
  21. // ブロードキャストインテントを受け取った時.
  22. @Override
  23. public void onReceive(Context context, Intent intent) {
  24. // TODO Auto-generated method stub
  25. // アクションを取得し, アクション文字列ごとに処理を分ける.
  26. String action = intent.getAction(); // intent.getActionでactionを取得.
  27. if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){ // actionがダウンロード完了なら.
  28. long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); // intentからダウンロードIDを取得.
  29. DownloadManager.Query query = new DownloadManager.Query(); // DownloadManager.Queryオブジェクトquery作成.
  30. //query.setFilterById(id); // query.setFilterByIdでダウンロードIDをセットし, このIDでフィルタリング.
  31. query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL); // query.setFilterByStatusでDownloadManager.STATUS_SUCCESSFULでフィルタリング.
  32. Cursor c = this.mainActivity.downloadManager.query(query); // this.mainActivity.downloadManager.queryにqueryをセットして, Cursor型cを取得.
  33. if (c.moveToFirst()){ // カーソルの最初に移動.
  34. int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); // ステータスを取得.
  35. // ステータスが成功ならトースト表示.
  36. if (status == DownloadManager.STATUS_SUCCESSFUL){ // 成功なら.
  37. Toast.makeText(this.mainActivity, "STATUS_SUCCESSFUL", Toast.LENGTH_LONG).show(); // "STATUS_SUCCESSFUL"と表示.
  38. }
  39. }
  40. c.close();
  41. }
  42. }
  43. }
Add Comment
Please, Sign In to add comment