作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
能否请您告诉我,如何为 Android 制作 Wake On Lan 应用程序?我在谷歌上搜索了两个星期,尝试了所有方法,从另一个局域网唤醒应用程序下载了源代码,并试图找到用于制作和发送魔术包的代码。看起来所有其他代码都有效,但是当我在我的应用程序中使用它时,它不起作用!而且我还尝试编辑 Java 代码以使其适用于 Android(此:http://www.jibble.org/wake-on-lan/WakeOnLan.java)。你能帮我吗?对不起我的英语不好。这是我的代码:
package com.macura.wakemypc;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import com.macura.wakemypc.MainActivity;
import com.macura.wakemypc.R;
import com.macura.wakemypc.MainActivity;
import com.macura.wakemypc.MainActivity;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public static final int PORT = 9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void buttonClick(View view) {
EditText iptext = (EditText)findViewById(R.id.ipbox);
EditText mactext = (EditText)findViewById(R.id.macbox);
String broadcastIP = String.valueOf(iptext.getText());
String mac = String.valueOf(mactext.getText());
Log.d("Read mac= ", mac);
Log.d("Read ip=", broadcastIP);
MainActivity.wakeup(broadcastIP, mac);
}
private static byte[] getMacBytes(String mac) throws IllegalArgumentException {
Log.d("GetMacBytes", "method started");
// TODO Auto-generated method stub
byte[] bytes = new byte[6];
try {
String hex;
for (int i = 0; i < 6; i++) {
hex = mac.substring(i*2, i*2+2);
bytes[i] = (byte) Integer.parseInt(hex, 16);
Log.d("GetMacbytes", "calculated");
Log.d("GetMacBytes (bytes)", new String(bytes));
}
}
catch (NumberFormatException e) {
Log.e("GetMacBytes","error");
}
return bytes;
}
public static void wakeup(String broadcastIP, String mac) {
Log.d("wakeup", "method started");
if (mac == null) {
Log.d("Mac error at wakeup", "mac = null");
return;
}
try {
byte[] macBytes = getMacBytes(mac);
Log.d("wakeup (bytes)", new String(macBytes));
byte[] bytes = new byte[6 + 16 * macBytes.length];
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) 0xff;
}
for (int i = 6; i < bytes.length; i += macBytes.length) {
System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
}
Log.d("wakeup", "calculating completed, sending...");
InetAddress address = InetAddress.getByName(broadcastIP);
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, 9);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
Log.d("wakeup", "Magic Packet sent");
}
catch (Exception e) {
Log.e("wakeup", "error");
}
}
}
这是 Logcat 输出(我用 X 代替 mac):
07-07 14:58:36.282: D/Read mac=(7247): XX:XX:XX:XX:XX:XX
07-07 14:58:36.282: D/Read ip=(7247): 192.168.1.255
07-07 14:58:36.282: D/wakeup(7247): method started
07-07 14:58:36.282: D/GetMacBytes(7247): method started
07-07 14:58:36.282: D/GetMacbytes(7247): calculated
07-07 14:58:36.282: D/GetMacBytes (bytes)(7247): ������������
07-07 14:58:36.282: E/GetMacBytes(7247): error
07-07 14:58:36.282: W/System.err(7247): java.lang.NumberFormatException: Invalid int: ":1"
07-07 14:58:36.282: W/System.err(7247): at java.lang.Integer.invalidInt(Integer.java:138)
07-07 14:58:36.282: W/System.err(7247): at java.lang.Integer.parse(Integer.java:375)
07-07 14:58:36.282: W/System.err(7247): at java.lang.Integer.parseInt(Integer.java:366)
07-07 14:58:36.282: W/System.err(7247): at com.macura.wakemypc.MainActivity.getMacBytes(MainActivity.java:57)
07-07 14:58:36.282: W/System.err(7247): at com.macura.wakemypc.MainActivity.wakeup(MainActivity.java:77)
07-07 14:58:36.282: W/System.err(7247): at com.macura.wakemypc.MainActivity.buttonClick(MainActivity.java:47)
07-07 14:58:36.282: W/System.err(7247): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:58:36.282: W/System.err(7247): at java.lang.reflect.Method.invoke(Method.java:511)
07-07 14:58:36.282: W/System.err(7247): at android.view.View$1.onClick(View.java:3586)
07-07 14:58:36.292: W/System.err(7247): at android.view.View.performClick(View.java:4084)
07-07 14:58:36.292: W/System.err(7247): at android.view.View$PerformClick.run(View.java:16966)
07-07 14:58:36.292: W/System.err(7247): at android.os.Handler.handleCallback(Handler.java:615)
07-07 14:58:36.292: W/System.err(7247): at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 14:58:36.292: W/System.err(7247): at android.os.Looper.loop(Looper.java:137)
07-07 14:58:36.292: W/System.err(7247): at android.app.ActivityThread.main(ActivityThread.java:4931)
07-07 14:58:36.292: W/System.err(7247): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:58:36.292: W/System.err(7247): at java.lang.reflect.Method.invoke(Method.java:511)
07-07 14:58:36.292: W/System.err(7247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-07 14:58:36.292: W/System.err(7247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
07-07 14:58:36.292: W/System.err(7247): at dalvik.system.NativeStart.main(Native Method)
07-07 14:58:36.292: D/wakeup (bytes)(7247): ������������
07-07 14:58:36.292: D/wakeup(7247): calculating completed, sending...
07-07 14:58:36.292: E/wakeup(7247): error
07-07 14:58:36.292: W/System.err(7247): android.os.NetworkOnMainThreadException
07-07 14:58:36.292: W/System.err(7247): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-07 14:58:36.302: W/System.err(7247): at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:175)
07-07 14:58:36.302: W/System.err(7247): at libcore.io.IoBridge.sendto(IoBridge.java:473)
07-07 14:58:36.302: W/System.err(7247): at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182)
07-07 14:58:36.302: W/System.err(7247): at java.net.DatagramSocket.send(DatagramSocket.java:284)
07-07 14:58:36.302: W/System.err(7247): at com.macura.wakemypc.MainActivity.wakeup(MainActivity.java:91)
07-07 14:58:36.302: W/System.err(7247): at com.macura.wakemypc.MainActivity.buttonClick(MainActivity.java:47)
07-07 14:58:36.302: W/System.err(7247): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:58:36.302: W/System.err(7247): at java.lang.reflect.Method.invoke(Method.java:511)
07-07 14:58:36.302: W/System.err(7247): at android.view.View$1.onClick(View.java:3586)
07-07 14:58:36.302: W/System.err(7247): at android.view.View.performClick(View.java:4084)
07-07 14:58:36.302: W/System.err(7247): at android.view.View$PerformClick.run(View.java:16966)
07-07 14:58:36.302: W/System.err(7247): at android.os.Handler.handleCallback(Handler.java:615)
07-07 14:58:36.302: W/System.err(7247): at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 14:58:36.302: W/System.err(7247): at android.os.Looper.loop(Looper.java:137)
07-07 14:58:36.312: W/System.err(7247): at android.app.ActivityThread.main(ActivityThread.java:4931)
07-07 14:58:36.312: W/System.err(7247): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:58:36.312: W/System.err(7247): at java.lang.reflect.Method.invoke(Method.java:511)
07-07 14:58:36.312: W/System.err(7247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-07 14:58:36.312: W/System.err(7247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
07-07 14:58:36.312: W/System.err(7247): at dalvik.system.NativeStart.main(Native Method)
最佳答案
看来您使用了错误的方法从 edittext (toString)
获取文本,您必须使用 gettext
。
关于java - 如何为 Android 制作 Wake On Lan?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17392245/
我是一名优秀的程序员,十分优秀!