首页 > 其他分享 >Android11 实现侧键打印走纸

Android11 实现侧键打印走纸

时间:2022-12-13 21:24:45浏览次数:91  
标签:mPrinterService 走纸 Android11 public paper import 侧键 android void

/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java else if (keyCode == KeyEvent.KEYCODE_F12) {             //XCSW wenjie.gu add for Side key printer paper start             if(down && repeatCount==0){                 Intent intentslide = new Intent("com.xcheng.printerservice.wrappaper");         // The length of the paper can be changed by changing the int value here  for example-->intentslide.putExtra("length_paper",10);----XCSW wenjie.gu add                 intentslide.putExtra("length_paper",5);                 intentslide.addFlags(0x01000000);                 mContext.sendBroadcast(intentslide);             }             //XCSW wenjie.gu add for Side key printer paper end         }



  /SlideKeyReceiver.java

package printerservice;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import javax.security.auth.callback.Callback;

public class SlideKeyReceiver extends BroadcastReceiver {
private final String TAG="SlidekeyReceiver";
private IPrinterCallback mCallback = null;
private IPrinterService mPrinterService;
private boolean mPrintConnected = false;
private Context mContext;
private int paper_Len;
@Override
public void onReceive(Context context, Intent intent) {
mContext=context;
paper_Len=intent.getIntExtra("length_paper",5);
if(mPrinterService==null){
onPrinterStart();

}

}

public void onPrinterStart(){
mCallback = new IPrinterCallback.Stub() {
@Override
public void onException(int code, final String msg) throws RemoteException {
Log.w(TAG, "onException("+ code+","+msg+")");
}
@Override
public void onLength(long current, long total) throws RemoteException {

}
public void onComplete() {
Log.i(TAG, "onComplete()");
}
@Override
public void onRealLength(double realCurrent, double realTotal) throws RemoteException {

}
};
Intent intent=new Intent();
intent.setPackage("com.xcheng.printerservice");
intent.setAction("com.xcheng.printerservice.IPrinterService");
mContext.startService(intent);
mContext.getApplicationContext().bindService(intent, mConnectionService, Context.BIND_AUTO_CREATE);

}
private ServiceConnection mConnectionService = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mPrinterService = null;
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mPrinterService = IPrinterService.Stub.asInterface(service);
onPrinterConnected();
if(printerPaper()){
Wrappaper(paper_Len,mCallback);
}
}
};
public void onPrinterConnected(){
printerInit();
mPrintConnected = true;
}
public void printerInit(){
try {
if(mPrinterService!=null){
mPrinterService.printerInit(mCallback);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean printerPaper(){
try {
if(mPrinterService!=null){
return mPrinterService.printerPaper(mCallback);
}
}catch(Exception e){

}
return false;
}
public void Wrappaper(int n ,IPrinterCallback callback){
try {
if(mPrinterService!=null){
mPrinterService.printWrapPaper(n,callback);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}
AndroidManifest.xml
<receiver android:name=".SlideKeyReceiver">
<intent-filter>
<action android:name="com.printerservice.wrappaper"/>
</intent-filter>
</receiver>
 
 

标签:mPrinterService,走纸,Android11,public,paper,import,侧键,android,void
From: https://www.cnblogs.com/gwj0424/p/16980638.html

相关文章