模拟器设置能够使用下载管理器

时间:2015-01-14 10:19:14

标签: android

我需要更改模拟器中的哪些设置才能使用DownloadManager类下载,因为每当我尝试在模拟器上运行代码时应用程序都会崩溃? 这是我正在使用的代码 Starter方法是一种启动下载的按钮单击事件方法。

public class MainActivity extends Activity {
    DownloadManager dm;
    EditText e;
    Uri uri;
    Long reference,ref;
    IntentFilter i1;
    DownloadManager.Request req;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e=(EditText)findViewById(R.id.editText1);
        i1=new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED);
        dm=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
        BroadcastReceiver rec= new BroadcastReceiver() {

            @Override
            public void onReceive(Context arg0, Intent arg1) {
                // TODO Auto-generated method stub
                ref=arg1.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

                // Setting Dialog Title
                alertDialog.setTitle("Continue download?");

                // Setting Dialog Message
                alertDialog.setMessage("Do you want this download to continue?");


                // Setting Positive "Yes" Button
                alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int which) {

                    // Write your code here to invoke YES event
                    //Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
                    }
                });

                // Setting Negative "NO" Button
                alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to invoke NO event
                    dm.remove(ref);
                    Toast.makeText(getApplicationContext(), "Download aborted",Toast.LENGTH_LONG).show();       
                    dialog.cancel();
                    }
                });

                // Showing Alert Message
                alertDialog.show();
            }
        };
        registerReceiver(rec, i1);

    }

    public void starter()
    {
        //String url=e.getText().toString();
        //if(url !="" & url!="")
        {
            uri=Uri.parse("http://commonsware.com/misc/test.mp4");
            Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
            .mkdirs();
            //req=;

            reference=dm.enqueue(new DownloadManager.Request(uri)
            .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"test.mp4"));

        }

    }

1 个答案:

答案 0 :(得分:0)

您没有处理异常。使用try catch块下载文件。检查互联网连接是否可用。如果互联网连接可用,则仅继续下载并向用户显示警告对话框,该对话框要求连续性。否则取消下载。

此外,如果您使用具有代理设置的互联网连接,则还需要为模拟器设置这些代理设置。