有需要可以去 https://github.com/L0ngD3/BroadcastBestPractice.git 下载
1.首先创建一个类用来管理所有的活动。
1 public class ActivityCollector { 2 public static List<Activity> activities = new ArrayList<>(); 3 public static void addActivity(Activity activity){ 4 activities.add(activity); 5 } 6 public static void removeActivity(Activity activity){ 7 activities.remove(activity); 8 } 9 public static void finishAll(){ 10 for (Activity activity : activities){ 11 if (!activity.isFinishing()){ 12 activity.finish(); 13 } 14 } 15 } 16 }
2.创建一个类作为活动的父类
public class BaseActivity extends AppCompatActivity { private ForceOffLine receive; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityCollector.addActivity(this); }protected void onDestroy() { super.onDestroy(); ActivityCollector.removeActivity(this); } }
3.创建登录的布局
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:orientation="vertical" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <LinearLayout 6 android:layout_width="match_parent" 7 android:layout_height="90dp"> 8 <TextView 9 android:layout_gravity="center_vertical" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="账号:" 13 android:textSize="20dp" 14 /> 15 <EditText 16 android:id="@+id/account" 17 android:layout_width="0dp" 18 android:layout_height="wrap_content" 19 android:layout_weight="1" 20 android:layout_gravity="center_vertical" 21 android:hint="请输入账号" 22 /> 23 </LinearLayout> 24 25 <LinearLayout 26 android:layout_width="match_parent" 27 android:layout_height="90dp"> 28 <TextView 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:text="密码:" 32 android:textSize="20dp" 33 /> 34 <EditText 35 android:id="@+id/password" 36 android:layout_width="0dp" 37 android:layout_height="wrap_content" 38 android:layout_weight="1" 39 android:hint="请输入密码" 40 /> 41 </LinearLayout> 42 <Button 43 android:id="@+id/login" 44 android:layout_width="match_parent" 45 android:layout_height="wrap_content" 46 android:text="登录" 47 android:textSize="20dp" 48 /> 49 </LinearLayout>
4.创建一个类用来判断登录
public class LoginActivity extends BaseActivity { EditText password; EditText account; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lohin);
//找到控件 account = (EditText) findViewById(R.id.account); password = (EditText) findViewById(R.id.password); Button login = (Button) findViewById(R.id.login);
//登录 login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String admin= account.getText().toString().trim(); String pwd= password.getText().toString().trim(); if (admin.equals("")||pwd.equals("")){ Toast.makeText(getApplicationContext(),"帐号或者密码为空",Toast.LENGTH_SHORT).show(); }else if (admin.equals("admin")&&pwd.equals("123456")){ //实现跳转 在登录界面跳转到主函数上去 你可以将MainActivity理解为登录后的界面 Intent intent = new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); }else { Toast.makeText(getApplicationContext(),"帐号或者密码错误",Toast.LENGTH_SHORT).show(); } } }); }
5.在main布局中设置一个点击按钮,点击后就会跳转到登录界面
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="欢迎进入系统" android:textSize="30dp" android:textColor="#ff00" /> <Button android:id="@+id/offLine" android:text="下线" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" />
6.实现跳转就是发送了一条广播,所以我们创建广播接收器来接收这条广播,实现跳转,并且弹出对话框给出提示。修改BaseActivity类
1 public class BaseActivity extends AppCompatActivity { 2 private ForceOffLine receive; 3 @Override 4 protected void onCreate(@Nullable Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 ActivityCollector.addActivity(this); 7 } 8 9 //注册活动 10 protected void onResume() { 11 super.onResume(); 12 //广播 13 IntentFilter intentFilter = new IntentFilter();
//添加我们所需要的广播 14 intentFilter.addAction("com.example.a13522.xiaxian.FORCE_OFFLINE"); 15 receive = new ForceOffLine(); 16 registerReceiver(receive,intentFilter); 17 18 } 19 protected void onPause() { 20 super.onPause(); 21 if (receive!=null){ 22 unregisterReceiver(receive); 23 receive = null; 24 } 25 } 26 //取消 27 protected void onDestroy() { 28 super.onDestroy(); 29 ActivityCollector.removeActivity(this); 30 } 31 32 33 //定义广播 34 class ForceOffLine extends BroadcastReceiver{ 35 public void onReceive(final Context context, final Intent intent) {
//实现tan框的功能 36 AlertDialog.Builder builder = new AlertDialog.Builder(context); 37 builder.setTitle("警告"); 38 builder.setMessage("你的账户多地登录,请修改密码重新登录"); 39 builder.setCancelable(false);//false表示不可取消
//创建点击按钮事件 40 builder.setNegativeButton("OK", new DialogInterface.OnClickListener() { 41 @Override 42 public void onClick(DialogInterface dialog, int which) { 43 ActivityCollector.finishAll();//销毁所有活动 44 //从此界面跳转到登录界面 45 Intent intent= new Intent(context,LoginActivity.class); 46 //重新启动活动 47 context.startActivity(intent); 48 } 49 }); 50 builder.show(); 51 } 52 } 53 }
最后在清单文件中设置主布局
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.a13522.xiaxian"> 3 4 <application 5 android:allowBackup="true" 6 android:icon="@mipmap/ic_launcher" 7 android:label="@string/app_name" 8 android:roundIcon="@mipmap/ic_launcher_round" 9 android:supportsRtl="true" 10 android:theme="@style/AppTheme"> 11 <activity android:name=".MainActivity"> 12 </activity> 13 <activity android:name=".LoginActivity"> 14 <intent-filter> 15 <action android:name="android.intent.action.MAIN" /> 16 <category android:name="android.intent.category.LAUNCHER" /> 17 </intent-filter> 18 </activity> 19 </application> 20 21 </manifest>
执行过程:首先启动onCreate()和onResume()方法 创建活动和发送一条广播
点击按钮后:执行onPause(),取消一下,因为广播发送后必须取消一下。
在调用onCreate()和onResume()方法 创建活动和发送一条广播。点击下线并且点击了ok
先调用onPause(),,取消一下上一步创建活动和发送一条广播。
2.调用onCreate()和onResume()方法 创建活动和发送一条广播
3.调用onDestroy()因为点击ok后销毁所有活动,销毁后它跳转到登录界面重新创建一个活动