1. Wifi 的操作
扫描wifi的情况,并判断是否连接。
在布局文件中,加入一个开关按钮:
1.在Activity中,点击按钮,触发检测事件:
3. Intent传递自定义对象
有时候Intent传递数据时,基本数据类型已经不够用了,希望传递一些自定义的对象,请看下面这个例子:
传递对象要实现一个Parcelable接口,它是用来将传递的数据序列化用的。如本例传递一个Person类的对象:
Android中intent传递对象和Bundle的用法
扫描wifi的情况,并判断是否连接。
在布局文件中,加入一个开关按钮:
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_alignTop="@+id/button1"
android:layout_marginTop="63dp"
android:text="Switch" />
在Activity中操作:
public class MainActivity extends Activity {
private Button button;
private TextView textView;
private WifiManager manager;
private Switch switch1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得wifi的管理类
manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
button = (Button) this.findViewById(R.id.button1);
textView = (TextView) this.findViewById(R.id.wifi);
switch1 = (Switch)this.findViewById(R.id.switch1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//扫描到所有的wifi的信息
List<ScanResult> list = manager.getScanResults();
for (ScanResult result : list) {
textView.append("-->>" + result.SSID + "-->>"
+ result.level + "\n");
}
}
});
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//manager.getConnectionInfo();
manager.setWifiEnabled(isChecked);
}
});
}
}
在Manifest中加入权限:
<!-- 添加wifi的授权 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
2. 判断网络连接情况
1.在Activity中,点击按钮,触发检测事件:
manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
button = (Button) this.findViewById(R.id.button1);
textView = (TextView) this.findViewById(R.id.msg);
// 点击按钮判断网络的状况
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 只有两种情况:1、可用 2、不可用
// 判断手机的网络状况:wifi、mobile(2G和3G信号)
// 先判断是否有wifi
if (ConnectivityTools.isConnectity(MainActivity.this)) {
Toast.makeText(MainActivity.this, "网络正常!!", 1).show();
} else {
Toast.makeText(MainActivity.this, "网络异常!!", 1).show();
}
}
});
2. 自定义一个检测的类ConnectivityTools
public class ConnectivityTools {
private static ConnectivityManager manager;
private static final String TAG = "ConnectityTools";
public static boolean isConnectity(Context context) {
boolean flag = false;
manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
// 再判断是否有2G和3G信号
NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
Log.i(TAG, "有网络信号,并且可以传输数据!!");
flag = true;
} else {
Log.i(TAG, "没有信号!!");
flag = false;
}
return flag;
}
}
3.用到了检测网络状态的权限,所以要加入一些权限:
<!-- 添加网络授权 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
3. Intent传递自定义对象
有时候Intent传递数据时,基本数据类型已经不够用了,希望传递一些自定义的对象,请看下面这个例子:
传递对象要实现一个Parcelable接口,它是用来将传递的数据序列化用的。如本例传递一个Person类的对象:
public class Person implements Parcelable {
// private Serializable serializable;序列化的接口
private int age;
private String name;
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private Person(Parcel parcel) {
age = parcel.readInt();
name = parcel.readString();
}
public Person() {
}
public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
public Person createFromParcel(Parcel source) {
return new Person(source);
}
public Person[] newArray(int size) {
return new Person[size];
}
};
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
// 向Parcel写入要序列化的数据
dest.writeInt(age);
dest.writeString(name);
}
}
在MainActivity中用Intent传递Person对象:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,NextActivity.class);
// 实现向NextActivity传递一个复杂的对象:Person
Person person = new Person();
person.setAge(23);
person.setName("jack");
intent.putExtra("msg", person);
startActivity(intent);
}
});
在另一个Activity中接收数据:
public class NextActivity extends Activity {
private TextView textView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
textView = (TextView) this.findViewById(R.id.msg);
Person person = (Person) getIntent().getParcelableExtra("msg");
textView.setText("-->>" + person.toString());
}
}
Android中intent传递对象和Bundle的用法