下面列出了android.graphics.pdf.PdfDocument.Page#android.print.PrintManager 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Print the current web site
*/
@RequiresApi(19)
public void printWebView(@Nullable PrintListener listener) {
PrintManager printManager = (PrintManager) getContext().getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter = createPrintDocumentAdapter();
PrintAttributes.Builder builder = new PrintAttributes.Builder();
builder.setMediaSize(PrintAttributes.MediaSize.NA_LETTER);
if (printManager != null) {
PrintJob printJob = printManager.print(getTitle(), printAdapter, builder.build());
if (listener!=null) {
if(printJob.isCompleted()){
listener.onSuccess();
} else {
listener.onFailed();
}
}
} else {
if (listener != null) {
listener.onFailed();
}
}
}
private void printNote() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
PrintManager printManager = (PrintManager) activity.getSystemService(Context.PRINT_SERVICE);
String jobName = getString(R.string.app_name) + " Document";
// GitHub issue 197
if(editor != null){
PrintDocumentAdapter printAdapter = editor.createPrintDocumentAdapter();
printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
}else if(editText != null){
printEditText(jobName);
}else{
Toast.makeText(activity, R.string.empty_note_description, Toast.LENGTH_LONG).show();
}
}
}
/**
* Print a {@link WebView}'s contents, also allows to create a PDF
*
* @param webview WebView
* @param jobName Name of the job (affects PDF name too)
* @return {{@link PrintJob}} or null
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@SuppressWarnings("deprecation")
public PrintJob print(WebView webview, String jobName) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
PrintDocumentAdapter printAdapter;
PrintManager printManager = (PrintManager) webview.getContext().getSystemService(Context.PRINT_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
printAdapter = webview.createPrintDocumentAdapter(jobName);
} else {
printAdapter = webview.createPrintDocumentAdapter();
}
if (printManager != null) {
return printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
}
} else {
Log.e(getClass().getName(), "ERROR: Method called on too low Android API version");
}
return null;
}
private void genPdfFile() {
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
printAdapter = mWebView.createPrintDocumentAdapter("my.pdf");
} else {
printAdapter = mWebView.createPrintDocumentAdapter();
}
// Create a print job with name and adapter instance
String jobName = getString(R.string.app_name) + " Document";
PrintAttributes attributes = new PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(new PrintAttributes.Resolution("id", Context.PRINT_SERVICE, 200, 200))
.setColorMode(PrintAttributes.COLOR_MODE_COLOR)
.setMinMargins(PrintAttributes.Margins.NO_MARGINS)
.build();
printManager.print(jobName, printAdapter, attributes);
}
@Override
public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
IPrintManager service = null;
// If the feature not present, don't try to look up every time
if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
service = IPrintManager.Stub.asInterface(ServiceManager
.getServiceOrThrow(Context.PRINT_SERVICE));
}
final int userId = ctx.getUserId();
final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid);
return new PrintManager(ctx.getOuterContext(), service, userId, appId);
}
public static void print(Context ctx, WebView webView, String title) {
if (PalmUtils.isKitKat()) {
PrintManager printManager = (PrintManager) ctx.getSystemService(Context.PRINT_SERVICE);
if (PalmUtils.isLollipop()) {
printManager.print("Print_Assignment", webView.createPrintDocumentAdapter(title), null);
return;
}
printManager.print("Print_Assignment", webView.createPrintDocumentAdapter(), null);
}
}
private void createWebPrintJob(WebView webView) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getActivity()
.getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
// 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());
}
private void print_page(WebView view, String print_name, boolean manual) {
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter = view.createPrintDocumentAdapter(print_name);
PrintAttributes.Builder builder = new PrintAttributes.Builder();
builder.setMediaSize(PrintAttributes.MediaSize.ISO_A5);
PrintJob printJob = printManager.print(print_name, printAdapter, builder.build());
if(printJob.isCompleted()){
Toast.makeText(getApplicationContext(), R.string.print_complete, Toast.LENGTH_LONG).show();
}
else if(printJob.isFailed()){
Toast.makeText(getApplicationContext(), R.string.print_failed, Toast.LENGTH_LONG).show();
}
}
/**
* Print a {@link WebView}'s contents, also allows to create a PDF
*
* @param webview WebView
* @param jobName Name of the job (affects PDF name too)
* @return {{@link PrintJob}} or null
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public PrintJob print(final WebView webview, final String jobName, final boolean... landscape) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final PrintDocumentAdapter printAdapter;
final PrintManager printManager = (PrintManager) _context.getSystemService(Context.PRINT_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
printAdapter = webview.createPrintDocumentAdapter(jobName);
} else {
printAdapter = webview.createPrintDocumentAdapter();
}
final PrintAttributes.Builder attrib = new PrintAttributes.Builder();
if (landscape != null && landscape.length > 0 && landscape[0]) {
attrib.setMediaSize(new PrintAttributes.MediaSize("ISO_A4", "android", 11690, 8270));
attrib.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0));
}
if (printManager != null) {
try {
return printManager.print(jobName, printAdapter, attrib.build());
} catch (Exception ignored) {
}
}
} else {
Log.e(getClass().getName(), "ERROR: Method called on too low Android API version");
}
return null;
}
/**
* Print a {@link WebView}'s contents, also allows to create a PDF
*
* @param webview WebView
* @param jobName Name of the job (affects PDF name too)
* @return {{@link PrintJob}} or null
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public PrintJob print(final WebView webview, final String jobName, final boolean... landscape) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final PrintDocumentAdapter printAdapter;
final PrintManager printManager = (PrintManager) _context.getSystemService(Context.PRINT_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
printAdapter = webview.createPrintDocumentAdapter(jobName);
} else {
printAdapter = webview.createPrintDocumentAdapter();
}
final PrintAttributes.Builder attrib = new PrintAttributes.Builder();
if (landscape != null && landscape.length > 0 && landscape[0]) {
attrib.setMediaSize(new PrintAttributes.MediaSize("ISO_A4", "android", 11690, 8270));
attrib.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0));
}
if (printManager != null) {
try {
return printManager.print(jobName, printAdapter, attrib.build());
} catch (Exception ignored) {
}
}
} else {
Log.e(getClass().getName(), "ERROR: Method called on too low Android API version");
}
return null;
}
private void createWebPrintJob(WebView webView, String jobName) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getActivity()
.getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
// Create a print job with name and adapter instance
PrintJob printJob = printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
}
/**
* Starts a print job for the given WebView
*
* Source: https://developer.android.com/training/printing/html-docs.html
*
* @param v the WebView to be printed
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private void createWebPrintJob(WebView v) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter = new PrintDocumentAdapterWrapper(this, v.createPrintDocumentAdapter());
// Create a print job with name and adapter instance
printJob = printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
}
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.KITKAT)
private void createWebPrintJob(WebView webView) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getSystemService(PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
// Create a print job with name and adapter instance
String jobName = getString(R.string.document, getString(R.string.app_name));
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
public Object createService(ContextImpl ctx) {
IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE);
IPrintManager service = IPrintManager.Stub.asInterface(iBinder);
return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
UserHandle.getAppId(Process.myUid()));
}
public Object createService(ContextImpl ctx) {
IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE);
IPrintManager service = IPrintManager.Stub.asInterface(iBinder);
return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
UserHandle.getAppId(Process.myUid()));
}
public Object createService(ContextImpl ctx) {
IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE);
IPrintManager service = IPrintManager.Stub.asInterface(iBinder);
return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
UserHandle.getAppId(Process.myUid()));
}
@TargetApi(19)
public static PrintManager getPrintManager() {
return (PrintManager) getSystemService(Context.PRINT_SERVICE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rescuecode_show);
try {
final EntropyHarvester entropyHarvester = EntropyHarvester.getInstance();
SQRLStorage storage = SQRLStorage.getInstance(RescueCodeShowActivity.this.getApplicationContext());
storage.newRescueCode(entropyHarvester);
List<String> rescueArr = storage.getTempShowableRescueCode();
final TextView txtRescueCodeShowDescription = findViewById(R.id.txtRescueCodeShowDescription);
final ViewGroup rootView = findViewById(R.id.rescueCodeShowActivityView);
final Button btnRescueCodeShowNext = findViewById(R.id.btnRescueCodeShowNext);
txtRescueCodeShowDescription.setMovementMethod(LinkMovementMethod.getInstance());
RescueCodeInputHelper rescueCodeInputHelper = new RescueCodeInputHelper(
this, rootView, btnRescueCodeShowNext, false);
rescueCodeInputHelper.setInputEnabled(false);
rescueCodeInputHelper.setRescueCodeInput(rescueArr);
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
findViewById(R.id.btnRescueCodeShowNext).setOnClickListener(v -> {
startActivity(new Intent(this, RescueCodeEnterActivity.class));
});
findViewById(R.id.btnPrintRescueCode).setOnClickListener(v -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
String jobName = getString(R.string.app_name) + " Document";
PrintAttributes printAttributes = new PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.build();
printManager.print(jobName, new RescueCodePrintDocumentAdapter(this), printAttributes);
} else {
showPrintingNotAvailableDialog();
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_export_options);
setupProgressPopupWindow(getLayoutInflater());
setupErrorPopupWindow(getLayoutInflater());
final CheckBox cbWithoutPassword = findViewById(R.id.cbWithoutPassword);
findViewById(R.id.btnShowIdentity).setOnClickListener(v -> {
Intent showIdentityIntent = new Intent(this, ShowIdentityActivity.class);
showIdentityIntent.putExtra(EXPORT_WITHOUT_PASSWORD, cbWithoutPassword.isChecked());
startActivity(showIdentityIntent);
});
findViewById(R.id.btnSaveIdentity).setOnClickListener(v -> {
String uriString = "content://org.ea.sqrl.fileprovider/sqrltmp/";
File directory = new File(getCacheDir(), "sqrltmp");
if(!directory.exists()) directory.mkdir();
if(!directory.exists()) {
showErrorMessage(R.string.main_activity_could_not_create_dir);
return;
}
SQRLStorage storage = SQRLStorage.getInstance(ExportOptionsActivity.this.getApplicationContext());
try {
File file = File.createTempFile("identity", ".sqrl", directory);
byte[] saveData;
if(cbWithoutPassword.isChecked()) {
saveData = storage.createSaveDataWithoutPassword();
} else {
saveData = storage.createSaveData();
}
FileOutputStream FileOutputStream = new FileOutputStream(file);
FileOutputStream.write(saveData);
FileOutputStream.close();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(uriString + file.getName()));
shareIntent.setType("application/octet-stream");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.save_identity_to)));
} catch (Exception e) {
showErrorMessage(e.getMessage());
Log.e(TAG, e.getMessage(), e);
}
});
findViewById(R.id.btnPrintIdentity).setOnClickListener(v -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
String jobName = getString(R.string.app_name) + " Document";
PrintAttributes printAttributes = new PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.build();
long currentId = SqrlApplication.getCurrentId(this.getApplication());
String identityName = mDbHelper.getIdentityName(currentId);
printManager.print(jobName, new IdentityPrintDocumentAdapter(this, identityName, cbWithoutPassword.isChecked()), printAttributes);
} else {
showPrintingNotAvailableDialog();
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_graph:
Intent i = new Intent(getApplicationContext(), GraphActivity.class);
i.putExtra(GraphActivity.GRAPH_SESSION, new Session(editableMonth, editableYear, editableCurrency));
i.putExtra(GraphActivity.GRAPH_UPDATE_MONTH, updateMonth);
i.putExtra(GraphActivity.GRAPH_UPDATE_YEAR, updateYear);
startActivity(i);
return true;
case R.id.action_show_months:
startActivity(new Intent(this, MonthActivity.class));
return true;
case R.id.action_print:
if (table.getChildCount() > 1) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
PrintManager printM = (PrintManager) getSystemService((Context.PRINT_SERVICE));
String job = getString(R.string.app_name) + ": " +
(!isSelectedMonthOlderThanUpdate()?
getString(MONTH_STRINGS[editableMonth]):getString(MONTH_STRINGS[updateMonth]));
printM.print(job,
new PPrintDocumentAdapter(this, table, new Session(editableMonth, editableYear,
currencyName), new int[]{updateMonth, updateYear}),
null);
}
} else {
Snackbar.make(table, getString(R.string.nothing_to_print), Snackbar.LENGTH_SHORT).show();
}
return true;
case R.id.action_donate:
startActivity(new Intent(getApplicationContext(), DonateActivity.class));
return true;
case R.id.action_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
case R.id.action_feedback:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
Uri.fromParts("mailto", getString(R.string.email), null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT,
getString(R.string.feedback_about, getString(R.string.app_name)));
emailIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.mail_content));
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{getString(R.string.email)});//makes it work in 4.3
Intent chooser = Intent.createChooser(emailIntent, getString(R.string.choose_email));
chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(chooser);//prevents exception
startActivity(chooser);
return true;
}
return super.onOptionsItemSelected(item);
}
public PrintManagerDelegateImpl(Activity activity) {
mPrintManager = (PrintManager) activity.getSystemService(Context.PRINT_SERVICE);
}