How do I specify and add a custom printer in an Android app?

前端 未结 1 1969
挽巷
挽巷 2021-02-04 21:48

I\'m creating an app for Android. Part of the desired app functionality is that the user can select a special printer (let\'s just call it Transfer Printer) which will pass on t

相关标签:
1条回答
  • 2021-02-04 22:26

    The trick I was missing was actually enabling the Print Service via the Android Settings menu. Actually doing this wasn't as straightforward as I would have hoped as the device manufacturer had removed the setting from the menu. It should be right under Accessibility in the System section of the menu.

    I ended up installing the Cloud Print app by Google, which gave me access to the Print Service settings temporarily (to enable the Cloud Print service). Once in here I noticed that my own service was, in fact, present.

    For posterity: To avoid un-installing and re-installing Cloud Print every time you want to change the Print Service settings, use the following SQLite3 commands, either with adb shell or from Terminal Emulator (or similar):

    sqlite3 data/data/com.android.providers.settings/databases/settings.db
    

    You should now have access to the Settings database and be using the SQLite3 command line shell. The settings of interest are located in the secure table and are enabled_print_services and enabled_on_first_boot_system_print_services. You can check if these settings already exist by using:

    .dump secure
    

    If they don't, then use the following commands:

    INSERT INTO secure VALUES(<id>, 'enabled_on_first_boot_system_print_services', 'com.companyname.appservice/com.companyname.appservice.TransferPrintService');
    INSERT INTO secure VALUES(<id>, 'enabled_print_services', 'com.companyname.appservice/com.companyname.appservice.TransferPrintService');
    

    You should, of course, replace 'com.companyname.appservice' with your own package and 'TransferPrintService' with your own print service. If these setting names do already exist, and your print service isn't listed, then you'll need to UPDATE instead of INSERT INTO:

    UPDATE secure SET value = '<existing print services>:<new print service>' WHERE name = 'enabled_on_first_boot_system_print_services';
    UPDATE secure SET value = '<existing print services>:<new print service>' WHERE name = 'enabled_print_services';
    

    You'll need to make sure to include any existing print services as part of the UPDATE command; listed print services are separated by a colon ":".

    Reboot the device to apply the updates to the settings database.

    0 讨论(0)
提交回复
热议问题