public class netReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent wifi = new Intent();
if(isWifi(context)){
wifi = new Intent(context, MyService.class);
context.startService(intent);
}else{
wifi.setAction("net.s5c.Changelink.wifiOff");
sendBroadcast(wifi);
}
Toast.makeText(context, "監聽到WIFI動作", Toast.LENGTH_LONG).show();
}
public static boolean isWifi(Context mContext) {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class netReceiver extends BroadcastReceiver {
Intent wifi = new Intent();
@SuppressWarnings("static-access")
public void onReceive(Context context, Intent intent) {
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = manager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == manager.TYPE_WIFI) {
wifi = new Intent(context, MyService.class);
wifi.setAction(MainActivity.ACCESSIBILITY_SERVICE);
context.startService(wifi);
}else{
wifi.setAction("net.s5c.Changelink.wifiOff");
context.sendBroadcast(wifi);
}
}
}
我又重寫了一下,發現廣播接收者多次接到廣播還是不能啟動起來服務,我知道我寫的有問題,但那裡有問題應該怎麼寫啊??