天天看点

关于广播的一个简单的demo

实现强制下线功能

1.首先创建一个Activity管理类

public class ActivityCollector {
    public static List<Activity>activities=new ArrayList<>();
    public static void addActivity(Activity activity){
        activities.add(activity);
    }
    public static void removeActivity(Activity activity){
        activities.remove(activity);
    }
    public static void finishAll(){
        for(Activity activity:activities){
            if(!activity.isFinishing()){
                activity.finish();
            }
        }
    }
}
           

2.然后BaseActivity类作为所有activity的父类

public class BaseActivity extends AppCompatActivity{

@Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        ActivityCollector.addActivity(this);
    }
}

 @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
           

3.首页设置一个模拟登录

public class MainActivity extends BaseActivity {
private EditText accountEdit;
private EditText passwordEdit;
private Button login;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        accountEdit=(EditText)findViewById(R.id.account);
        passwordEdit=(EditText)findViewById(R.id.password);
        login=(Button)findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account=accountEdit.getText().toString().trim();
                String password=passwordEdit.getText().toString().trim();
                //如果帐号是admin 密码是123456
                if(account.equals("admin")&&password.equals("123456")){
                    Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    Toast.makeText(MainActivity.this,"帐秘错误",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}
           

4跳转到第二页

第二页是个按钮,点击按钮发送

public class SecondActivity extends BaseActivity {
Button mButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        mButton=(Button)findViewById(R.id.button2);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent("666");
                sendBroadcast(intent);
            }
        });
    }
}
           

然后我们在baseActivity中动态注册BroadcastReceiver

public class BaseActivity extends AppCompatActivity{
    private ForceOffLineReceiver receiver;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("666");
        receiver=new ForceOffLineReceiver();
        registerReceiver(receiver,intentFilter);

    }

    @Override
    protected void onPause() {
        super.onPause();
        if(receiver!=null){
            unregisterReceiver(receiver);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
    class ForceOffLineReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(final Context context, Intent intent) {
            AlertDialog.Builder builder=new AlertDialog.Builder(context);
            builder.setTitle("warning");
            builder.setMessage("异地登陆警告");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    ActivityCollector.finishAll();
                    Intent intent1=new Intent(context,MainActivity.class);
                    context.startActivity(intent1);
                }
            });
            builder.show();
        }
    }

}
           
关于广播的一个简单的demo
关于广播的一个简单的demo

例子比较简单,现在看下EventBus