PrintManager Android显示黑屏

时间:2016-07-07 08:32:41

标签: android printing android-5.0-lollipop

我正在尝试创建一个项目,其中包含一个附加了函数的按钮来调用PrintManager。我试过谷歌的guide但是当我点击按钮时它显示黑屏并且没有任何反应,没有崩溃或任何事情。我的代码是:

import android.content.Context;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintJob;
import android.print.PrintManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private WebView mWebView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button myButton = (Button) findViewById(R.id.Print_Button);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doWebViewPrint();
            }
        });

    }

    private void doWebViewPrint() {
        // Create a WebView object specifically for printing
        WebView webView = new WebView(this);
        webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i("MYAPP-------------", "page finished loading " + url);
               // createWebPrintJob(view);
                mWebView = null;
            }
        });


        webView.loadUrl("http://developer.android.com/about/index.html");

        // Keep a reference to WebView object until you pass the PrintDocumentAdapter
        // to the PrintManager
        mWebView = webView;
    }

    private void createWebPrintJob(WebView webView) {

        // Get a PrintManager instance
        PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);

        // Get a print adapter instance
        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter("MyDocument");

        // Create a print job with name and adapter instance
        String jobName = getString(R.string.app_name) + " Document";
        PrintJob printJob = printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
    }

}

我已经将API设置为21,并且我在使用Android 5.0的Samsung Galaxy S4上运行它,所以从理论上讲,它应该可行。

我还检查了“Log.i(”MYAPP -------------“,”页面已完成加载“+ url);”出现在控制台中,因此按钮正在工作。

0 个答案:

没有答案