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 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader