我是Android开发的新手。
我编写了一个可以打开/关闭WiFi的应用程序。
这是我的代码:
MainActivity.java
package com.narun.wifi_control;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context context = getApplicationContext();
final CharSequence wifioff = "Wifi is OFF";
final CharSequence wifion = "Wifi is ON";
final int duration= Toast.LENGTH_SHORT;
final WifiManager wifiman=(WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE);
ToggleButton tb = (ToggleButton) findViewById(R.id.toggleButton1);
ConnectivityManager connman = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean wifi = connman.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
if(wifi){
tb.setChecked(true);
Toast toast= Toast.makeText(context, wifion, duration);
toast.show();
}
else{
tb.setChecked(false);
Toast toast = Toast.makeText(context, wifioff, duration);
toast.show();
}
tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
wifiman.setWifiEnabled(true);
Toast toast= Toast.makeText(context, wifion, duration);
toast.show();
}
else{
wifiman.setWifiEnabled(false);
Toast toast = Toast.makeText(context, wifioff, duration);
toast.show();
}
}
});
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.narun.wifi_control.MainActivity"
tools:ignore="MergeRootFrame" >
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:text="ToggleButton" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Wifi Control"
android:textAppearance="?android:attr/textAppearanceLarge" />
和我的清单文件
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.narun.wifi_control"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<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"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.narun.wifi_control.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>
但我想将它作为主屏幕小部件,当我们点击时,它应该从主屏幕打开/关闭Wifi,我该如何实现?