編輯:關於Android編程


class SnmpManager {
private TransportMapping transportMapping = null;
private Snmp snmp = null;
private int version;
public final static int version1 = SnmpConstants. version1;
public final static int version2c = SnmpConstants.version2c;
public final static int version3 = SnmpConstants. version3;
/**
* 構造方法
* @param version
*/
public SnmpManager( int version) {
this. version = version;
try {
// 設置成Udp協議
transportMapping = new DefaultUdpTransportMapping();
snmp = new Snmp( transportMapping);
if (version == version3) {
// 設置安全信息
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels. getInstance().addSecurityModel(usm);
}
transportMapping.listen();
} catch (Exception e) {
e.printStackTrace();
System. out.println(e);
}
}
/**
* @param sync
* @param bro
* @param pdu
* @param addr
* 發送消息方法
*/
public void sendMsg(boolean sync, final boolean bro, PDU pdu, String addr) {
Address targetAddres = GenericAddress. parse(addr);
Target target = null;
if ( this. version == version3) {
snmp.getUSM().addUser( new OctetString( "MD5DES"), new UsmUser( new OctetString( "MD5DES"), AuthMD5. ID, new OctetString("MD5DESUserAuthPassword" ), PrivDES.ID, new OctetString("MD5DESUserPrivPassword" )));
target = new UserTarget();
// 設置安全級別
target.setSecurityLevel(SecurityLevel. AUTH_PRIV);
target.setSecurityName( new OctetString("MD5DES"));
target.setVersion(SnmpConstants. version3);
} else {
target = new CommunityTarget();
if ( this. version == version1) {
target.setVersion( version1);
((CommunityTarget) target).setCommunity(new OctetString("public" ));
} else {
target.setVersion( version2c);
((CommunityTarget) target).setCommunity(new OctetString("public" ));
}
}
target.setAddress(targetAddres);
target.setRetries(2);
target.setTimeout(1000);
if (sync) {
// 發送報文 並且接受響應
ResponseEvent response = null;
try {
response = snmp.send(pdu, target);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System. out.println(e);
}
// 處理響應
System. out.println( "Synchronize message from " + response.getPeerAddress() + "/nrequest:" + response.getRequest() + "/nresponse:" + response.getResponse());
} else {
ResponseListener listener = new ResponseListener() {
@Override
public void onResponse(ResponseEvent event) {
if (!bro) {
((Snmp) event.getSource()).cancel(event.getRequest(), this );
}
// 處理響應
PDU request = event.getRequest();
PDU response = event.getResponse();
System. out.println( "Asynchronise message from " + event.getPeerAddress() + "/nrequest:" + request + "/nresponse:" + response);
}
};
try {
snmp.send(pdu, target, null, listener);
} catch (IOException e) {
e.printStackTrace();
System. out.println(e);
}
}
}
}
public class SnmpTest {
public static String myVersion = "";
static boolean sync = false;
static boolean bro = false;
/**
* 主函數
* @param args
*/
public static void main(String[] args) {
SnmpManager manager = new SnmpManager(SnmpConstants.version2c );
// 構造報文
PDU pdu = new PDU();
// PDU pdu = new ScopedPDU(); version3使用
// 設置要獲取的對象ID
OID oids = new OID( "1.3.6.1.2.1.1.1.0");
// OID oids = new OID(new int [] { 1, 3, 6, 1, 2, 1, 1, 1, 0 });
pdu.add( new VariableBinding(oids));
// 設置報文類型
pdu.setType(PDU. GET);
// ((ScopedPDU) pdu).setContextName(new OctetString("priv"));
// 發送消息 其中最後一個是想要發送的目標地址
manager.sendMsg( true, true, pdu, "udp:127.0.0.1/161");
}
}

private void buttonFun() {
noNull();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
api = new SnmpAPI();
session = new SnmpSession(api);
SnmpPDU pdu = new SnmpPDU();
UDPProtocolOptions protocol = new UDPProtocolOptions();
protocol.setRemoteHost(getText(target));
protocol.setRemotePort(Integer.parseInt(getText(port)));
pdu.setProtocolOptions(protocol);
// Build Get request PDU
pdu.setCommand(SnmpAPI.GET_REQ_MSG);
pdu.setCommunity(getText(community));
if(version.getSelectedItemPosition()==0){
pdu.setVersion(SnmpAPI.SNMP_VERSION_1);
}else if(version.getSelectedItemPosition()==1){
pdu.setVersion(SnmpAPI.SNMP_VERSION_2C);
}else if(version.getSelectedItemPosition()==2){
//還需設置MD5 SHA認證密碼等
pdu.setVersion(SnmpAPI.SNMP_VERSION_3);
}
pdu.setTimeout(Integer.parseInt(getText(timeout)));
pdu.setRetries(Integer.parseInt(getText(retries)));
// SnmpOID oid = new SnmpOID("1.3.6.1.2.1.1.1.0");
// SnmpOID oid = new SnmpOID((SnmpOID)(getText(oid)));
// pdu.addNull(oid);
String oidstr = getText(oid);
String str[] = oidstr.split("\\.");
int a[] = new int[9];
for(int i=0; i
做到這份上,跟華為的老師聯系上了,他們跟我說年前比較忙,拖到年後才能詳談需求業務,我也暫時擱置此項目,去做別的事了。也許有新的情況,我會寫個第二篇,希望對大家有幫助。
AsyncTask陷阱之:Handler,Looper與MessageQueue的詳解
AsyncTask的隱蔽陷阱先來看一個實例這個例子很簡單,展示了AsyncTask的一種極端用法,挺怪的。復制代碼 代碼如下:public class AsyncTask
Android使用WindowManager制作一個可拖動的控件
效果圖如下第一步:新建DragView繼承RelativeLayoutpackage com.rong.activity;import com.rong.test.R;i
Android自定義豎直方向SeekBar多色進度條
寫在前面因為有這樣的一個場景,需要實現豎直方向的多色進度條,然後在網上也找了下,沒看到符合需要的,於是自定義了一個,效果如下: 具體實現本來想定義水平的,然後旋轉一下,後
MSM8909+Android5.1.1之BSP開發---電池管理2---BatteryInfo.java
MSM8909+Android5.1.1之BSP開發---電池管理2---BatteryInfo.java 先來借用MTK對電池管理的框架圖 圖1&n