天天看點

關于廣播的一個簡單的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