公共類WifiPrint延伸活動{TextView的打印機名稱, gate_way,printer_port; int port = 9100; WifiManager wifi = null; android.net.DhcpInfo d;字符串gateway_ip =「」;
@Override保護無效的onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_wifi_print); wifi =(WifiManager) getSystemService(Context.WIFI_SERVICE); printer_name =(TextView) findViewById(R.id.textView1); gate_way =(TextView) findViewById(R.id.textView2); printer_port =(TextView) findViewById(R.id.textView3); /* de.lohndirekt.print.IppPrintService svc = new IppPrintService(printerURI); InputStream stream = new BufferedInputStream(new FileInputStream(「image.epl」)); DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; Doc myDoc = new SimpleDoc(stream,flavor,null); DocPrintJob job = svc.createPrintJob(); job.print(myDoc,NULL); */
}
@覆蓋公共布爾onCreateOptionsMenu(菜單菜單){// 充氣菜單;這會將項目添加到操作欄(如果存在)。 getMenuInflater()。inflate(R.menu.wifi_print,menu);返回true; }
公共無效wifisettings(視圖v){startActivityForResult(新 意圖( android.provider.Settings.ACTION_WIFI_SETTINGS),0); }
公共無效connectSocket(視圖v){
/*烏里文件路徑= Uri.parse( 「android.resource://」 + getPackageName()+ 「/」 + R.raw.pdfsample); Toast.makeText(this,filepath.getPath(),Toast.LENGTH_LONG).show(); */ClientThread clientThread = new ClientThread(「」);線程clientstartThread = new 線程(clientThread); clientstartThread.start(); }
公共字符串intToIp(int i)以{返回(第(i &爲0xFF)+ 「」 +((I >> 8) &爲0xFF)+ 「」 +((I >> 16)& 0xFF的) +「。」+((i >> 24)& 0xFF)); }
@覆蓋保護無效onActivityResult(INT requestCode,整數 resultCode爲,意圖數據){// TODO自動生成方法存根
super.onActivityResult(requestCode, resultCode, data); if
(requestCode == 0){WifiInfo wifiinfo = wifi.getConnectionInfo (); d = wifi。getDhcpInfo();
printer_name.setText("Name:" + wifiinfo.getSSID()); gateway_ip =
intToIp(d.gateway); gate_way.setText(「Printer Ip:」+ gateway_ip); printer_port.setText(「Port:」+ port);
} }
公共文件createFileFromInputStream(InputStream的的inputStream){
try{
File f = new File("/sdcard/testpdf.pdf");
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;
while((length=inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,length);
}
outputStream.close();
inputStream.close();
return f;
}catch (IOException e) {
//Logging exception
}
return null; }
類ClientThread實現Runnable {字符串文件路徑= 「」; BufferedInputStream bis = null; FileInputStream fis;
Socket socket;
public ClientThread(String filename) { filepath = filename; }
@Override public void run() { // TODO Auto-generated method
存根嘗試{
的InputStream在= getResources()openRawResource(R.raw.pdfsample)。 createFileFromInputStream(in); File pdfFile = new File(「/ sdcard/testpdf.pdf」); byte [] mybytearray = new byte [(int)pdfFile.length()]; socket = new Socket(gateway_ip,port); runOnUiThread(新的Runnable(){
@Override 公共無效的run(){// TODO自動生成方法存根 如果(socket.isConnected()){ Toast.makeText(WifiPrint.this, 「插座連接「, Toast.LENGTH_LONG).show();
}
} });
fis = new FileInputStream(pdfFile); bis = new BufferedInputStream(fis); bis.read(mybytearray,0,mybytearray.length); OutputStream os = socket.getOutputStream(); os.write(mybytearray,0,mybytearray.length); os.flush();
if (bis != null) {
bis.close();
os.close();
socket.close();
fis.close();
}
} catch (final UnknownHostException e) {
// TODO Auto-generated catch block
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(WifiPrint.this,
"UnHost Exceptions" + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}); } catch (final IOException e) {
// TODO Auto-generated catch block
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(WifiPrint.this,
"Io Exceptions" + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}); } finally {
try {
if (bis != null) {
bis.close();
socket.close();
fis.close();
}
} catch (final IOException e) {
// TODO Auto-generated catch block
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(
WifiPrint.this,
"Io Exeption in Closing Socket"
+ e.getMessage(), Toast.LENGTH_LONG)
.show();
}
});
} } }
}
}
http://stackoverflow.com/questions/9745859/networkonmainthread – ASP
使用 「StrictMode」,但不建議.... – ASP
@ user3632006請問上面的代碼工作? – VVB