PIPI0 2016-01-22 01:27 采纳率: 85.7%
浏览 1728
已采纳

请问广播接受者不能发送广播吗?还是我写的代码有问题啊?

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;  
}  

}

  • 写回答

3条回答 默认 最新

  • PIPI0 2016-01-22 05:09
    关注
    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);
            }
    
        }
    }
    
    

    我又重写了一下,发现广播接收者多次接到广播还是不能启动起来服务,我知道我写的有问题,但那里有问题应该怎么写啊??

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序