編輯:關於android開發
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yanlei.wifi" > <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 在SDCard中創建與刪除文件權限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 連接<a href="http://www.it165.net/news/nhlw/" target="_blank" class="keylink">互聯網</a>Internet權限 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- GPS定位權限 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".WifiListActivity" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </RelativeLayout>
item_wifi_list.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView" android:layout_marginBottom="14dp" android:layout_toRightOf="@+id/imageView" android:text="TextView" /> <TextView android:id="@+id/signal_strenth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView" android:layout_alignBottom="@+id/textView" android:layout_alignParentRight="true" android:text="TextView" /> </RelativeLayout>
MainActivity.java
package com.example.yanlei.wifi; import android.content.Context; import android.net.wifi.ScanResult; import android.net.wifi.WifiManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.List; public class MainActivity extends AppCompatActivity { private WifiManager wifiManager; List<ScanResult> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); openWifi(); list = wifiManager.getScanResults(); ListView listView = (ListView) findViewById(R.id.listView); if (list == null) { Toast.makeText(this, "wifi未打開!", Toast.LENGTH_LONG).show(); }else { listView.setAdapter(new MyAdapter(this,list)); } } /** * 打開WIFI */ private void openWifi() { if (!wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(true); } } public class MyAdapter extends BaseAdapter { LayoutInflater inflater; List<ScanResult> list; public MyAdapter(Context context, List<ScanResult> list) { // TODO Auto-generated constructor stub this.inflater = LayoutInflater.from(context); this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view = null; view = inflater.inflate(R.layout.item_wifi_list, null); ScanResult scanResult = list.get(position); TextView textView = (TextView) view.findViewById(R.id.textView); textView.setText(scanResult.SSID); TextView signalStrenth = (TextView) view.findViewById(R.id.signal_strenth); signalStrenth.setText(String.valueOf(Math.abs(scanResult.level))); ImageView imageView = (ImageView) view.findViewById(R.id.imageView); //判斷信號強度,顯示對應的指示圖標 if (Math.abs(scanResult.level) > 100) { imageView.setImageDrawable(getResources().getDrawable(R.drawable.a0)); } else if (Math.abs(scanResult.level) > 80) { imageView.setImageDrawable(getResources().getDrawable(R.drawable.a1)); } else if (Math.abs(scanResult.level) > 70) { imageView.setImageDrawable(getResources().getDrawable(R.drawable.a0)); } else if (Math.abs(scanResult.level) > 60) { imageView.setImageDrawable(getResources().getDrawable(R.drawable.a1)); } else if (Math.abs(scanResult.level) > 50) { imageView.setImageDrawable(getResources().getDrawable(R.drawable.a0)); } else { imageView.setImageDrawable(getResources().getDrawable(R.drawable.a1)); } return view; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
來自:android wifi講解 wifi列表顯示
XListView,xlistview用法第一步:到官網下載第三方包,拷貝到自己的項目中 https://github.com/Maxwin-z/XListView-A
GsonFormat插件從配置到使用,gsonformat插件配置說明:目前多數服務器端都以json格式返回,那麼相對應的解析時建立的實體類如果你還在自己挨個寫的話,那就
Apktool(3)——Apktool的使用,apktool使用一.apktool的作用 安卓應用apk文件不僅僅是包含有resource和編譯的java代碼的zip文件
(Android Studio實用指南)8.16 提煉參數對象 某些參數總是同時出現,可能好幾個方法都使用這樣一組參數,為了避免參數列表過長,同時也為了避免重復代碼,