2015-11-05 45 views
0

我正在研究哦如何触发我的应用程序,如果任何下载发生在任何应用程序。我有使用DownloadManager的代码片段,只有当我的应用程序执行任何下载时,才会通知我,但我从来没有找到任何解决方案,在我的移动设备上发生任何下载,必须通知我的应用程序。 Pl建议我,如果这将是可能的。谢谢接收器下载 - Android

+0

您可以使用BroadCastReceiver的。 –

+0

你可以给我简要的解释。欣赏,如果你有任何参考。 – RAAAAM

+0

这可能有助于http://stackoverflow.com/questions/22950893/how-to-detect-if-a-specific-app-is-being-downloaded-installed-by-the-google-play –

回答

0

1-您必须制作Service才能下载后台中的任何文件。

public class DownloadService extends Service { 

    private static final String TAG = "DownloadService"; 
    public static final int UPDATE_PROGRESS = 8344; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     if (intent == null) { 
     } else { 

      final String urlToDownload = intent.getStringExtra("url"); 
      final ResultReceiver receiver = (ResultReceiver) intent 
        .getParcelableExtra("receiver"); 

      new Thread(new Runnable() { 

       @Override 
       public void run() { 
        try { 
         URL url = new URL(urlToDownload); 
         URLConnection connection = url.openConnection(); 
         connection.connect(); 

         int fileLength = connection.getContentLength(); 

         // download the file 
         InputStream input = new BufferedInputStream(url 
           .openStream()); 
         String localPath = Environment 
           .getExternalStorageDirectory() 
           .getAbsoluteFile() 
           + File.separator 
           + Constant.ROOT_FOLDER_NAME 
           + File.separator 
           + Constant.FOLDER_IMAGE 
           + File.separator 
           + urlToDownload.substring(urlToDownload 
             .lastIndexOf('/') + 1); 

         AppLog.Log(TAG, "Path :: " + localPath); 

         OutputStream output = new FileOutputStream(localPath); 

         byte data[] = new byte[1024]; 
         long total = 0; 
         int count; 
         while ((count = input.read(data)) != -1) { 
          total += count; 
          // publishing the progress.... 
          Bundle resultData = new Bundle(); 
          resultData.putInt("progress", 
            (int) (total * 100/fileLength)); 
          receiver.send(UPDATE_PROGRESS, resultData); 
          output.write(data, 0, count); 
         } 

         output.flush(); 
         output.close(); 
         input.close(); 
        } catch (IOException e) { 
         AppLog.Log(TAG, "********* EXCEPTION *****"); 
         e.printStackTrace(); 
        } 

        Bundle resultData = new Bundle(); 
        resultData.putInt("progress", 100); 
        receiver.send(UPDATE_PROGRESS, resultData); 
        stopSelf(); 
       } 
      }).start(); 
     } 

     return START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
} 

2 - 那么你必须做出ResultReceiver得到通知,而下载完成。

private class DownloadReceiver extends ResultReceiver { 

    public DownloadReceiver(Handler handler) { 
     super(handler); 
    } 

    @Override 
    protected void onReceiveResult(int resultCode, Bundle resultData) { 
     super.onReceiveResult(resultCode, resultData); 
     if (resultCode == DownloadService.UPDATE_PROGRESS) { 
      int progress = resultData.getInt("progress"); 

      if (progress == 100) { 
       // Download Complete 
      } 
     } 
    } 
} 

3-呼叫下载服务

Intent intent = new Intent(context, DownloadService.class); 
intent.putExtra("url", "url to download"); 
intent.putExtra("receiver", new DownloadReceiver(new Handler())); 
startService(intent); 
  • 变化清单文件应用标签里面的Manifest.xml

添加标签:

<application > 
------ 
------ 

<service android:name="PACKAGENAME.DownloadService" /> 

------ 
------ 
</application>