施工中.....

Android權限設定

. . . . .

關於uses-permission

各式各樣的uses-permission

首先找到AndroidManifest.xml這個資料夾

在AndroidManifest.xml加入Wi-Fi讀取權限與更改Wi-Fi權限

介面設計

ScrollView語法

若是內容高度超過android:layout_height="?"的話 , 就會以滾動方式呈現

增加一段android:scrollbars="none"的話就不會顯示滾動條

程式碼

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<Button
    android:id="@+id/but1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Search Wi-Fi"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="135dp"
    tools:layout_editor_absoluteY="447dp" />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="16dp">




    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="WiFiDemo" />
    </ScrollView>

</LinearLayout>

JAVA程式

this

我們回到上面的this

一開始寫出來因為java原本沒有這個函數

所以我們必須對紅底按alt+Enter來撰寫功能

這邊this是<普通的直接引用>

更多this,super用法 . . . .

BroadcastReceiver

通常廣播是用來通知系統

此範例中則是用來通知使用者沒開啟WIFI

這邊將它設為receiver然後在下面撰寫它的功能

廣播接收元件

super

關於super用法

程式碼

package com.example.user.wifi_2;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.IntentFilter;

import android.net.wifi.WifiConfiguration;

import android.net.wifi.WifiInfo;

import android.net.wifi.WifiManager;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;

import java.util.List;

import static com.example.user.wifi_2.R.id.info;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static final String TAG="繃繃包"; WifiManager wifi; BroadcastReceiver receiver;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 TextView text;

 Button  but;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text=(TextView)findViewById(R.id.text);
    but=(Button)findViewById(R.id.but1);
    but.setOnClickListener(this);

    wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
    WifiInfo info=wifi.getConnectionInfo();
    text.append("\n\nwifi status ;"+info.toString());




    List<WifiConfiguration> configurations=wifi.getConfiguredNetworks();
    for(WifiConfiguration configuration :configurations){
        text.append("\n\n" +configuration.toString());
    }

    if(receiver==null)
        receiver=new WifiScaner(this);
    registerReceiver(receiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    Log.d(TAG, "onCreate()");

}

@Override
public void onStop() {
    unregisterReceiver(receiver);
    super.onStop();


}
@Override
public void onClick(View view) {
    Toast.makeText(getApplicationContext(),"ALL WIFI seached~~",0).show();
    if(view.getId()==R.id.but1){
        Log.d(TAG, "onCreat() wifi.startScan()");
        wifi.startScan();
    }

}

}

接著對WifiScaner按alt+Enter寫立一個新的java檔

程式碼

package com.example.user.wifi_2;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.net.wifi.WifiManager;

import android.nfc.Tag;

import android.util.Log;

import android.widget.Toast;

import java.util.List;

class WifiScaner extends BroadcastReceiver {

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
private  static final String TAG="WifiscanReceiver";
MainActivity main;


public WifiScaner(MainActivity mainActivity) {
    super();
    this.main=main;

}


@Override
public void onReceive(Context context, Intent intent) {
    List<android.net.wifi.ScanResult> results=main.wifi.getScanResults();
    android.net.wifi.ScanResult bestsignal =null;
    for(android.net.wifi.ScanResult result:results){
        if(bestsignal==null|| WifiManager.compareSignalLevel(bestsignal.level, result.level)<0)
            bestsignal=result;
    }

    String message=String.format("%s networks found.%s is the strongest.",results.size(), results.size(),bestsignal.SSID);
    Toast.makeText(main, message, 0).show();
    Log.d(TAG, "onReceive() message :" +message);


}

}


Comments

comments powered by Disqus