android的意图

  这是动态广播
  IntentFilter filter = new IntentFilter();
  filter.addAction(WifiManager.ACTION_PICK_WIFI_NETWORK);
  registerReceiver(new WifiReceiver(), filter);

//静态广播直接可以这样用
 Intent intent = new Intent();
    intent.setAction("cn.programmer.CUSTOM_INTENT");
      intent.setComponent( new ComponentName( "com.app.myapplication" ,
        "com.app.myapplication.MyReceiver") );//系统高级点的需要指明包名
    sendBroadcast(intent);

   // 动态接收广播接收
class WifiReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        WifiManager mWifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        String action = intent.getAction();
       //textView.append(action);
        Log.i("WifiReceiver", action);
        // / Wifi 状态变化
        if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
            WifiInfo info = mWifiManager.getConnectionInfo();
            SupplicantState state = info.getSupplicantState();
            if (state == SupplicantState.COMPLETED) {
               // textView.append("(验证成功)");
                Log.i("WifiReceiver", "(验证成功)");
            }
            int errorCode = intent.getIntExtra(
                    WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
            if (errorCode == WifiManager.ERROR_AUTHENTICATING) {
              //  textView.append("(验证失败)");
                Log.i("WifiReceiver", "(验证失败)");
            }
        }
    }
}
//动态广播之后要卸掉 应该是怕占用内存
   @Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(WifiReceiver);
}

//
//这是静态广播
public class MyBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(“COM.USER.LOGIN”)){
context.startActivity(new Intent(context, LoginActivity.class));
}
}
//需要在androidMainfest里面注册


      
          
          
      
  

你可能感兴趣的:(android,androdi)