哪位高手能帮本人看一下,监听类就是当前activity,sdk版本是分别是16和17.小弟刚开始学安卓,很多都不太懂,希望高手帮忙解答一下。
public class MainActivity extends FragmentActivity implements OnPageChangeListener, OnCheckedChangeListener, NsdManager.DiscoveryListener,NsdManager.ResolveListener, NsdManager.RegistrationListener { private ViewPager pager; private MainPageAdapter adapter; public List<Fragment> fragments; private RadioGroup group; private RadioButton button0, button1, button2,button3; private NsdManager mNsdManager; private NsdManager.ResolveListener mResolveListener; private String TAG = "NSD_LOG "; private boolean isDescovering = false; public boolean backtoroot = false; public HashMap<String,Fragment> fragmentsMap = new HashMap<String,Fragment>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragments = new ArrayList<Fragment>(); fragments.add(new Fragment_A()); fragments.add(new ColorSelectOneActivity()); fragments.add(new Fragment_C()); fragments.add(new Fragment_D()); pager = (ViewPager) findViewById(R.id.pager); adapter = new MainPageAdapter(getSupportFragmentManager(), fragments); pager.setAdapter(adapter); pager.setOffscreenPageLimit(fragments.size() - 1); pager.setOnPageChangeListener(this); group = (RadioGroup) findViewById(R.id.radioGroup1); button0 = (RadioButton) findViewById(R.id.radio0); button1 = (RadioButton) findViewById(R.id.radio1); button2 = (RadioButton) findViewById(R.id.radio2); button3 = (RadioButton) findViewById(R.id.radio3); group.setOnCheckedChangeListener(this); WifiSwitcher wifiSwitcher = new WifiSwitcher(); fragmentsMap.put("wifi_switcher",wifiSwitcher); StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build()); //自动连接,先判断能否是AP,假如不是则连接mdns AppDelegate delegate = (AppDelegate)getApplication(); try { Socket socket = new Socket("192.168.1.1", 5001); delegate.put("host","192.168.1.1"); delegate.put("port", 5001); } catch (Exception e) { e.printStackTrace(); // 开始自动发现网络服务 // 获取nsdmanager try { mNsdManager = (NsdManager)getSystemService(NSD_SERVICE); // // NsdServiceInfo serviceInfo = new NsdServiceInfo(); // serviceInfo.setServiceName("WokeSmart"); // serviceInfo.setServiceType("_socket._tcp"); // serviceInfo.setPort(5001); // // // serviceInfo.setHost(InetAddress.getLocalHost()); //开始发现 // mNsdManager.discoverServices("_ipp._tcp.", mNsdManager.PROTOCOL_DNS_SD, this); // mNsdManager.discoverServices("_http._tcp", mNsdManager.PROTOCOL_DNS_SD, this); // mNsdManager.discoverServices("_socket._tcp.local", mNsdManager.PROTOCOL_DNS_SD, this); mNsdManager.discoverServices("_socket._tcp", mNsdManager.PROTOCOL_DNS_SD, this); // mNsdManager.registerService(serviceInfo,mNsdManager.PROTOCOL_DNS_SD,this); }catch (Exception e1) { e1.printStackTrace(); } } } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub Log.v("asdf", "onPageScrollStateChanged"); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub // Log.v("asdf", "onPageScrolled"); } @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub Log.v("asdf", "onPageSelected"); getTabState(arg0); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub switch (checkedId) { case R.id.radio0: pager.setCurrentItem(0); break; case R.id.radio1: pager.setCurrentItem(1); break; case R.id.radio2: pager.setCurrentItem(2); break; case R.id.radio3: pager.setCurrentItem(3); break; default: break; } } private void getTabState(int index) { // TODO Auto-generated method stub button0.setChecked(false); button1.setChecked(false); button2.setChecked(false); button3.setChecked(false); switch (index) { case 0: button0.setChecked(true); break; case 1: button1.setChecked(true); break; case 2: button2.setChecked(true); break; case 3: button3.setChecked(true); break; default: break; } } public void onKeyBack(int i) { if (i>0) i--; else return; if (pager.getCurrentItem()==0) { Fragment_A fragment_a = (Fragment_A)fragments.get(pager.getCurrentItem()); ArrayList<Fragment> backStack = fragment_a.backStack; if (backStack.size()==1) { getSupportFragmentManager().beginTransaction().hide(backStack.get(backStack.size()-1)).commit(); } else { getSupportFragmentManager().beginTransaction().hide(backStack.get(backStack.size()-1)).show(backStack.get(backStack.size()-2)).commit(); } backStack.remove(backStack.size()-1); } else if (pager.getCurrentItem()==1) { ColorSelectOneActivity fragment_b = (ColorSelectOneActivity)fragments.get(pager.getCurrentItem()); ArrayList<Fragment> backStack = fragment_b.backStack; if (backStack.size()==1) { getSupportFragmentManager().beginTransaction().hide(backStack.get(backStack.size()-1)).commit(); } else { getSupportFragmentManager().beginTransaction().hide(backStack.get(backStack.size()-1)).show(backStack.get(backStack.size()-2)).commit(); } backStack.remove(backStack.size()-1); } onKeyBack(i); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { onKeyBack(1); return true; } return super.onKeyDown(keyCode, event); } /** * 设备像素(dip,dp)转屏幕像素(px) * px就是像素,假如用px,就会用实际像素画,比个如吧,用画一条长度为240px的横线,在480宽的模拟器上看就是一半的屏宽,而在320宽的模拟器上看就是2/3的屏宽了。 * 而dip,就是把屏幕的高分成480分,宽分成320分。例如你做一条160dip的横线,无论你在320还480的模拟器上,都是一半屏的长度。 * @param context * @param dipValue * @return */ public static int dip2px(Context context, float dipValue){ final float scale = context.getResources().getDisplayMetrics().density; return (int)(dipValue * scale + 0.5f); } /** * 将px值转换为sp值,保证文字大小不变 * * @param pxValue * @param pxValue * (DisplayMetrics类中属性scaledDensity) * @return */ public static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); } /** * 将sp值转换为px值,保证文字大小不变 * * @param spValue * @param spValue * (DisplayMetrics类中属性scaledDensity) * @return */ public static int sp2px(Context context, float spValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } //实现网络发现接口 // Called as soon as service discovery begins. @Override public void onDiscoveryStarted(String regType) { Log.d(TAG, "Service discovery started"); isDescovering = true; } @Override public void onServiceFound(NsdServiceInfo service) { // A service was found! Do something with it. if (service.getServiceName().contains("WokeSmart")) { // Service type is the string containing the protocol and // transport layer for this service. Log.d(TAG, "WokeSmart device found"); mNsdManager.resolveService(service, this); } } @Override public void onServiceLost(NsdServiceInfo service) { // When the network service is no longer available. // Internal bookkeeping code goes here. Log.e(TAG, "service lost" + service); } @Override public void onDiscoveryStopped(String serviceType) { Log.i(TAG, "Discovery stopped: " + serviceType); } @Override public void onStartDiscoveryFailed(String serviceType, int errorCode) { Log.e(TAG, "Discovery failed: Error code:" + errorCode); mNsdManager.stopServiceDiscovery(this); } @Override public void onStopDiscoveryFailed(String serviceType, int errorCode) { Log.e(TAG, "Discovery failed: Error code:" + errorCode); mNsdManager.stopServiceDiscovery(this); } @Override public void onServiceResolved(NsdServiceInfo arg0) { // 可以再这里获取相应网络服务的地址及端口信息,然后决定能否要与之建立连接。 // 之后就是一些socket操作了 //获取IP arg0.getHost(); //获取端口 arg0.getPort(); AppDelegate delegate = (AppDelegate)getApplication(); delegate.put("host", arg0.getHost().getAddress()); delegate.put("port", arg0.getPort()); } @Override public void onResolveFailed(NsdServiceInfo arg0, int arg1) { Log.e(TAG, "ResolveFailed failed: Error code:" + arg1); mNsdManager.stopServiceDiscovery(this); } @Override public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { Log.e(TAG, "register failed" + errorCode); } @Override public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { Log.e(TAG, "unregister failed" + errorCode); } @Override public void onServiceRegistered(NsdServiceInfo serviceInfo) { Log.e(TAG, "registed"); } @Override public void onServiceUnregistered(NsdServiceInfo serviceInfo) { Log.e(TAG, "unregisted"); } }
解决方案:20分
应该不是重启,而是虚拟机挂掉了
解决方案:80分
大哥,你写的代码也太坑了..你这桌子项目本人在接手,求指导决这个NsdManager – –