BroadcastReceiver正常工作(Android)

时间:2012-04-01 15:18:55

标签: java android broadcastreceiver intentservice

大家好日子! 在我的程序onReceive方法接收器StartServiceReceiver中,由程序启动(或旋转屏幕)触发,意图包含“android.net.wifi.STATE_CHANGE”。 怎么会这样? 感谢

public class KsivaFTPApacheActivity extends Activity {
Context context = null;
BroadcastReceiver StartService;
BroadcastReceiver GetStatus;
private final IntentFilter StartServiceFilter = new IntentFilter();
private final IntentFilter GetStatusFilter = new IntentFilter();
public static final String CUSTOM_INTENT = "com.imholynx.KsivaFTPApache.intent.start";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    StartServiceFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    StartServiceFilter.addAction(CUSTOM_INTENT);
    StartService = new StartServiceReceiver();  
    GetStatusFilter.addAction("AppService");
    GetStatus = new GetStatusReceiver();

    this.registerReceiver(StartService, StartServiceFilter);   
    this.registerReceiver(GetStatus, GetStatusFilter);
}
@Override
public void onResume() {
    super.onResume();   
}

@Override
public void onPause() {
    super.onPause();
 }

@Override
protected void onDestroy() {
    super.onDestroy();
    this.unregisterReceiver(StartService);
    this.unregisterReceiver(GetStatus);
    stopService(new Intent(this, KsivaFTPApacheService.class));     
}
public class StartServiceReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("State", intent.getAction());
        Bundle b = intent.getExtras();
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
        {

            NetworkInfo ni = null;
            ni = cm.getActiveNetworkInfo();
            if (ni != null)
                if (ni.isConnected()) {
                    Intent inten;
                    Log.i("INFO", ((Integer) Files.size()).toString());
                    if (!Files.isEmpty())
                        for (int i = 0; i < Files.size(); i++) {
                            if (Files.elementAt(i).Status == false) {
                                Log.i("INFO","send "+ Files.elementAt(i).FileName);
                                inten = new Intent(context,KsivaFTPApacheService.class);
                                Log.i("INFO", ((Integer) Files.size()).toString());
                                Log.i("INFO2", ((Integer) i).toString());
                                inten.putExtra("FileName",Files.elementAt(i).FileName);
                                inten.putExtra("Action",Files.elementAt(i).Action);
                                inten.putExtra("ID", i);
                                context.startService(inten);
                            }
                        }
                }
        }

    }

}

public class GetStatusReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("AppService")) {
            Log.i("AppService", ((Integer) intent.getIntExtra("Data", 1)).toString());
            Files.elementAt(intent.getIntExtra("Data", 1)).Status = true;
        }
    }

}   
}

2 个答案:

答案 0 :(得分:0)

因为您将意图添加到使用以下行传递给接收方的IntentFilter:

StartServiceFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);

在您注册接收器之前

答案 1 :(得分:0)

可能Manifest中没有intent过滤器。如果您无法找到它的来源,只需向接收器添加一些检查,如下所示:

@Override
public void onReceive(Context arg0, Intent arg1) {

if (! arg1.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
    return;
}

//....
}