想要调用第三方软件首先必须知道intent
这里使用逆向工具获取清单文件的IntentFilter
PrinterShare 清单
<activity android:configChanges="keyboardHidden|locale|orientation|screenSize" android:icon="@drawable/icon_title" android:name="com.dynamixsoftware.printershare.ActivityWeb"><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.VIEW"/><data android:scheme="file"/><data android:scheme="content"/><data android:mimeType="text/html"/></intent-filter><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.VIEW"/><data android:scheme="content"/><data android:scheme="file"/><data android:host="*"/><data android:pathPattern=".*\\.htm"/><data android:pathPattern=".*\\.html"/></intent-filter><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.SEND"/><data android:mimeType="text/*"/></intent-filter>
</activity>
<activity android:configChanges="keyboardHidden|locale|orientation|screenSize" android:icon="@drawable/icon_title" android:name="com.dynamixsoftware.printershare.ActivityPrintPDF"><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.VIEW"/><data android:scheme="content"/><data android:scheme="file"/><data android:mimeType="application/pdf"/></intent-filter><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.SEND"/><data android:mimeType="application/pdf"/></intent-filter><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.VIEW"/><data android:scheme="content"/><data android:scheme="file"/><data android:host="*"/><data android:pathPattern=".*\\.pdf"/></intent-filter></activity><activity android:configChanges="keyboardHidden|locale|orientation|screenSize" android:icon="@drawable/icon_title" android:name="com.dynamixsoftware.printershare.ActivityPrintDocuments"><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.VIEW"/><data android:scheme="content"/><data android:scheme="file"/><data android:mimeType="text/plain"/><data android:mimeType="application/vnd.ms-word"/><data android:mimeType="application/msword"/><data android:mimeType="application/doc"/><data android:mimeType="application/vnd.msword"/><data android:mimeType="application/winword"/><data android:mimeType="application/word"/><data android:mimeType="application/x-msw6"/><data android:mimeType="application/x-msword"/><data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document"/><data android:mimeType="application/vnd.ms-excel"/><data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/><data android:mimeType="application/vnd.ms-powerpoint"/><data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation"/><data android:mimeType="application/haansofthwp"/></intent-filter><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.SEND"/><data android:mimeType="message/rfc822"/><data android:mimeType="application/vnd.ms-word"/><data android:mimeType="application/msword"/><data android:mimeType="application/doc"/><data android:mimeType="application/vnd.msword"/><data android:mimeType="application/winword"/><data android:mimeType="application/word"/><data android:mimeType="application/x-msw6"/><data android:mimeType="application/x-msword"/><data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document"/><data android:mimeType="application/vnd.ms-excel"/><data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/><data android:mimeType="application/vnd.ms-powerpoint"/><data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation"/><data android:mimeType="application/haansofthwp"/></intent-filter><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.VIEW"/><data android:scheme="content"/><data android:scheme="file"/><data android:host="*"/><data android:pathPattern=".*\\.txt"/><data android:pathPattern=".*\\.doc"/><data android:pathPattern=".*\\.docx"/><data android:pathPattern=".*\\.xls"/><data android:pathPattern=".*\\.xlsx"/><data android:pathPattern=".*\\.ppt"/><data android:pathPattern=".*\\.pptx"/><data android:pathPattern=".*\\.hwp"/></intent-filter>
</activity>
<activity android:configChanges="keyboardHidden|locale|orientation|screenSize" android:icon="@drawable/icon_title" android:name="com.dynamixsoftware.printershare.ActivityPrintPictures"><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.VIEW"/><data android:scheme="content"/><data android:scheme="file"/><data android:mimeType="image/gif"/><data android:mimeType="image/png"/><data android:mimeType="image/jpeg"/></intent-filter><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.SEND"/><action android:name="android.intent.action.SEND_MULTIPLE"/><data android:mimeType="image/gif"/><data android:mimeType="image/png"/><data android:mimeType="image/jpeg"/></intent-filter><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.VIEW"/><data android:scheme="content"/><data android:scheme="file"/><data android:host="*"/><data android:pathPattern=".*\\.gif"/><data android:pathPattern=".*\\.png"/><data android:pathPattern=".*\\.jpeg"/><data android:pathPattern=".*\\.jpg"/></intent-filter><intent-filter><action android:name="com.sec.android.app.mobileprint.PRINT"/><action android:name="com.sec.android.app.mobileprint.PRINT_SETTING"/><category android:name="android.intent.category.DEFAULT"/></intent-filter>
</activity>
主要就4个Activity
- ActivityPrintPDF 打印PDF
- ActivityPrintDocuments 打印文档
- ActivityPrintPictures 打印图片
- ActivityWeb 打印网页
最后根据intent-filter 封装工具类
package me.leon;import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;import java.io.File;/*** <p>description:</p>* <p>author:Leon</p>* <p>date:2019/7/18 0018</p>* <p>e-mail:deadogone@gmail.com</p>*/
public class PrinterShareMgr {private static PrinterShareMgr mgr = new PrinterShareMgr();public static PrinterShareMgr getInstance() {return mgr;}private PrinterShareMgr() {}public void printFile(Context context, String filePath) {if (TextUtils.isEmpty(filePath)) {return;}Intent intent = new Intent(Intent.ACTION_VIEW);Uri data = Uri.fromFile(new File(filePath));ComponentName comp = null;if (filePath.endsWith("pdf")) {intent.setDataAndType(data,"application/pdf");comp = new ComponentName("com.dynamixsoftware.printershare", "com.dynamixsoftware.printershare.ActivityPrintPDF");} else if (filePath.endsWith("doc") || filePath.endsWith("docx")||filePath.endsWith("txt")){intent.setDataAndType(data,"application/doc");comp = new ComponentName("com.dynamixsoftware.printershare", "com.dynamixsoftware.printershare.ActivityPrintDocuments");}else if (filePath.endsWith("jpg") || filePath.endsWith("jpeg") || filePath.endsWith("gif") || filePath.endsWith("png")){intent.setDataAndType(data,"image/jpeg");comp = new ComponentName("com.dynamixsoftware.printershare", "com.dynamixsoftware.printershare.ActivityPrintPictures");}else if (filePath.endsWith("html")||filePath.endsWith("htm")){intent.setDataAndType(data,"text/html");comp = new ComponentName("com.dynamixsoftware.printershare", "com.dynamixsoftware.printershare.ActivityWeb");}intent.setComponent(comp);context.startActivity(intent);}
}
Note:
- 安装PrinterShare完成后 请自行更新对应打印机驱动
- 请自行兼容android 7.0 及补全未包含的文件格式
- 隐藏导航栏时,打印成功无法返回
参考
https://blog.csdn.net/qilin001cs/article/details/54616108
附件下载:
PrinterShare
AndroidKiller 1.3.1 csdn 蓝奏