如何制作'如果我按下按钮,下载管理器开启'?

时间:2014-10-18 09:13:02

标签: android

我将制作生根工具,但我想要按下按钮,下载管理器开启'码。请告诉我

我将在帮助下开始开发..

public class MainActivity extends Activity
{
    DownLoadComplte mDownload;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void startDownload() 
    {
    DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Request mRqRequest = new Request(Uri.parse("http://androidtrainningcenter.blogspot.in/2012/11/android-webview-loading-custom-html-and.html"));
    mRqRequest.setDescription("This is File");
    mRqRequest.setDestinationUri(Uri.parse("/sdcard/download"));
    long idDownLoad=mManager.enqueue(mRqRequest);


}
@Override
protected void onStart() {
    super.onStart();
    mDownload = new DownLoadComplte();
    registerReceiver(mDownload, new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(mDownload);
}

private class DownLoadComplte extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equalsIgnoreCase(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
            Toast.makeText(context, "Download Complte", Toast.LENGTH_LONG)
                    .show();
        }
    }
}}

XML CODE

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button 
        android:text="Start Download"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"/>
</LinearLayout>

感谢您提供任何帮助&amp;真的很感激

1 个答案:

答案 0 :(得分:0)

用以下代码替换您的代码: Java代码:

public void startDownload(View v) 
    {//do something..
     }

XML代码:     

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button 
        android:text="Start Download"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startDownload"/> // Change this
</LinearLayout>