放假在家颓废一个星期,不学习。之前的东西又忘的差不多了。慢慢学吧。哎!
1.创建ActivityCollector类用于管理活动
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<>();
public static void addActivity(Activity activity)
{
activities.add(activity);
}//添加活动到activities
public static void removeActivity(Activity activity)
{
activities.remove(activity);
}//从activities中删除活动
public static void finishAll()
{
for(Activity activity:activities)
{
if(!activity.isFinishing())
{
activity.finish();
}
}
activities.clear();
}
}
2.创建BaseActivity类作为所有活动的父类。
public class BaseActivity extends AppCompatActivity {
private ForceOfflineReceiver receiver;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
protected void onResume()
{
super.onResume();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("com.example.administrator.broadcastbestpractice.FORCE_OFFLINE");//创建intentfilter
receiver=new ForceOfflineReceiver();//创建一个离线广播监听器
registerReceiver(receiver,intentFilter);//注册监听器
}
protected void onPause()
{
super.onPause();
if(receiver!=null)
{
unregisterReceiver(receiver);//当每一次退出时取消注册监听器
receiver=null;
}
}
protected void onDestroy()
{
super.onDestroy();
ActivityCollector.removeActivity(this);
}
class ForceOfflineReceiver extends BroadcastReceiver{//定义广播监听类
public void onReceive(final Context context, Intent intent)
{
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("Wraning");
builder.setMessage("You are forced to be offline. Please try to login again.");
builder.setCancelable(false);
builder.setPositiveButton("OK",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which)
{
ActivityCollector.finishAll();
Intent intent = new Intent(context,LoginActivity.class);
context.startActivity(intent);
}
});
builder.show();
}
}
}
3.设计登陆界面
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="Account:"/>
<EditText
android:id="@+id/account"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="Password:"/>
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPassword"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Login"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
4.修改LoginActivity代码,实现登陆的逻辑判断。
public class LoginActivity extends BaseActivity {
private EditText accountEdit;
private EditText passwordEdit;
private Button login;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
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 v) {
String account=accountEdit.getText().toString();
String password=passwordEdit.getText().toString();
if(account.equals("admin")&&password.equals("123456"))
{
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}else
{
Toast.makeText(LoginActivity.this,"account or password is invaild",Toast.LENGTH_SHORT).show();
}
}
});
}
}
5.修改MainActivity中的代码,点击按钮实现强制下线。
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button forceOffline = (Button) findViewById(R.id.force_offline);
forceOffline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.example.administrator.broadcastbestpractice.FORCE_OFFLINE");
sendBroadcast(intent);
}
});
}
}
6.修改AndroidMainfest中代码
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
运行结果如图: