2016-03-29 23 views
-1

如何在後臺默默使用廣播接收器下載文件? 我想在每次啓動onReceive時下載一個文件。這也需要asynctask?下面是我的代碼如何使用廣播接收器下載文件?

package com.android.systemmanager; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class FreezerReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
//download wallpaper here 
} 
} 

我只是希望它下載的文件在下載目錄中,以便用戶可以從畫廊:)日後訪問

+0

使用內部的onReceive服務下載 –

+0

使用服務在後臺下載該文件。這將悄悄下載你想下載的文件。 –

回答

0

我建議,開始從廣播免費獲贈IntentServicebeacuse there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed-

請參閱this doc開始。

+0

這個任何片段?和下載程序;)預先感謝親愛的 – Emily

+0

@Emily閱讀 - http://developer.android.com/training/run-background-service/create-service.html –

+0

下載示例如果需要從上面的鏈接 –

0

嘗試這樣:

@Override 
protected void onHandleIntent(Intent intent) { 
    Log.e("ok","ok"); 
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    mBuilder = new NotificationCompat.Builder(this); 
    mBuilder.setContentTitle(
      "Picture Download dnsadg sadgasjdgashgd asgd asjdg asjgd sajgd s") 
      .setContentText("Download in progress") 
      .setSmallIcon(R.drawable.icon_app).setContentInfo("0%"); 

    mBuilder.setOngoing(true); 
    File root = new File(Environment.getExternalStorageDirectory() 
      + "/aaaa"); 
    String urlToDownload = "http://dl2.mid.az/endir.php?file=uploads/Exclusive/miri_yusif_-_yoxam_men_mp3.mid.az.mp3"; 
    // ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver"); 
    try { 
     URL url = new URL(urlToDownload); 
     URLConnection connection = url.openConnection(); 
     connection.connect(); 
     // this will be useful so that you can show a typical 0-100% progress bar 
     int fileLength = connection.getContentLength(); 

     // download the file 
     InputStream input = new BufferedInputStream(url.openStream()); 
     OutputStream output = new FileOutputStream(new File(root.getPath(), 
       "ok.mp3")); 

     byte data[] = new byte[1024]; 
     long total = 0; 
     int count; 
     while ((count = input.read(data)) != -1) { 
      total += count; 

      progressChange((int)(total * 100)/fileLength); 
      // 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) { 
     e.printStackTrace(); 
    } 

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

@Override 
public void onDestroy() { 
    Log.e("DESTROY", "DESTROY"); 
    super.onDestroy(); 
} 
void progressChange(int progress){ 


    if (lastupdate != progress) { 
     lastupdate = progress; 
     // not.contentView.setProgressBar(R.id.status_progress, 
     // 100,Integer.valueOf(progress[0]), false); 
     // inform the progress bar of updates in progress 
     // nm.notify(42, not); 
     if (progress < 100) { 
      mBuilder.setProgress(100, Integer.valueOf(progress), 
        false).setContentInfo(progress+"%"); 
      nm.notify(12, mBuilder.build()); 
       Intent i = new Intent("com.russian.apps.TabActivity").putExtra("some_msg",progress+"%"); 
       this.sendBroadcast(i); 
     } else { 
      mBuilder.setContentText("Download complete") 
      // Removes the progress bar 
        .setProgress(0, 0, false).setOngoing(false).setContentInfo("");; 

      nm.notify(12, mBuilder.build()); 

     } 

    } 

} 
} 

您可能要添加startForeground()方法來避免碰撞

+0

可以我將這整個代碼粘貼到我的onReceive方法中? – Emily

+0

除非您根據您的要求修改它,否則它將無法在您的代碼中使用....僅供參考 –