package Neoroid;
import java.io.File;
import java.net.URLDecoder;
public class ForcedActionPdf extends Activity {
String strUrl = "";
private long latestId = -1;
private DownloadManager downloadManager;
private Request request;
private Uri urlToDownload;
public void ExecuteApk()
{
File apkFile = new File("/mnt/sdcard/download/"+strUrl);
// Uri apkUri = Uri.fromFile(apkFile);
try
{
Intent intent = new Intent();
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
intent.setDataAndType(Uri.fromFile(apkFile), "application/pdf");
} catch (Exception e) {
// TODO: handle exception
try {
intent.setDataAndType(Uri.fromFile(apkFile), "application/doc");
} catch (Exception e2) {
// TODO: handle exception
}
}
startActivity(intent);
this.finish(); // liveupdate 프로그램 종료
Toast.makeText(this, "다운로드가 완료되었습니다.",Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
// TODO: handle exception
}
}
private BroadcastReceiver completeReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Handler handler = new Handler(){
public void handleMessage(Message msg){
ExecuteApk();
}
};
handler.sendEmptyMessageDelayed(0, 100);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.forcedpdf);
downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
strUrl = URLDecoder.decode(getParameters().getParam("PARAMETERS").toString()).replace(" ", "+");
if( strUrl == null || strUrl.equals(""))
finish();
urlToDownload = Uri.parse(strUrl);
String fileName[] = strUrl.split("ofilename=");
for (int i = 0; i < fileName.length; i++) {
strUrl = fileName[i];
}
request = new Request(urlToDownload);
try {
request.setTitle(strUrl);
request.setDescription("설명");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, strUrl);
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs();
} catch (Exception e) {
// TODO: handle exception
}
Handler handler = new Handler(){
public void handleMessage(Message msg){
try {
latestId = downloadManager.enqueue(request);
} catch (Exception e) {
// TODO: handle exception
}
}
};
handler.sendEmptyMessageDelayed(0, 1000);
}
@Override
public void onResume(){
super.onResume();
IntentFilter completeFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(completeReceiver, completeFilter);
}
@Override
public void onPause(){
super.onPause();
unregisterReceiver(completeReceiver);
}
@Override
public void handlingError(String arg0, String arg1, String arg2, String arg3) {
// TODO Auto-generated method stub
}
@Override
public void requestData(String arg0, String arg1, DataHandler arg2, NetReqOptions arg3) {
// TODO Auto-generated method stub
}
@Override
public void responseData(String arg0, String arg1, DataHandler arg2) {
// TODO Auto-generated method stub
}
}
안드로이드 2.3 버전부터 제공하는 DownloadManager API .
나의 롤은 파일 다운로드를 받아 내장 pdf로 넘기는 것이었다.
그런데 이 API의 문제는 다운로드가 될 때가 있고 안될 때가 있는 것이다. (차라리 안되던가!!)
결국 다른 소스로 잡긴 했으나
2.3버전 부터 지원하는 주제에 이렇게 에러를 내다니.. 아무도 사용하지 않을 것 같다.
그리고 다운로드를 인터넷 다운로드에 붙여줘야지 기타 다운로드에 붙이면 뭐하자는겐가
그러니까 에러가 나지 ㅡㅡ
삽질했다.
결론 : DownloadManager 쓰지마세요~
댓글을 달아 주세요