基本文件架构如下
ActivityColletor.java
import java.util.List; /** * */ public class ActivityCollector { private static List<Activity> activities = new ArrayList<Activity>(); 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(); } } } }
重建一个基类
package com.example.hellogxp.sharedpreference3; import android.app.Activity; import android.os.Bundle; import java.util.ArrayList; import java.util.List; /** * */ public class BaseActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityCollector.addActivity(this); } @Override protected void onDestroy() { super.onDestroy(); ActivityCollector.removeActivity(this); } }
强制下线的广播接收器
package com.example.hellogxp.sharedpreference3; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.view.Window; import android.view.WindowManager; /** * */ public class ForceOfflineReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("force offline"); builder.setMessage("you are force offline,please try login again"); builder.setCancelable(false); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { ActivityCollector.finishAll(); Intent intent = new Intent(context, LoginActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } }); AlertDialog alertDialog = builder.create(); alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); alertDialog.show(); } }
登录的业务逻辑
package com.example.hellogxp.sharedpreference3; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends BaseActivity { private SharedPreferences sharedPreferences; private SharedPreferences.Editor editor; private EditText editTextAccount, editTextPassword; private Button buttonRememberMe; private CheckBox checkBoxRememberMe; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); editTextAccount = (EditText) findViewById(R.id.account); editTextPassword = (EditText) findViewById(R.id.password); buttonRememberMe = (Button) findViewById(R.id.login); checkBoxRememberMe = (CheckBox) findViewById(R.id.remember_me); boolean isRemember = sharedPreferences.getBoolean("remember_me", false); if(isRemember){ String account = sharedPreferences.getString("account", ""); String password = sharedPreferences.getString("password", ""); editTextAccount.setText(account); editTextPassword.setText(password); checkBoxRememberMe.setChecked(true); } buttonRememberMe.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String account = editTextAccount.getText().toString(); String password = editTextPassword.getText().toString(); if(account.equals("hellogxp") && password.equals("123456")){ editor = sharedPreferences.edit(); if(checkBoxRememberMe.isChecked()){ editor.putString("account", account); editor.putString("password", password); editor.putBoolean("remember_me", true); } else { editor.clear(); } editor.commit(); Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } else { Toast.makeText(LoginActivity.this, "account or password invalid", Toast.LENGTH_SHORT).show(); } } }); } }
Main类
package com.example.hellogxp.sharedpreference3; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button buttonForceOffline = (Button) findViewById(R.id.force_offline); buttonForceOffline.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent("com.example.hellogxp.sharedpreference3.FORECE_OFFLINE"); sendBroadcast(intent); } }); } }
AndroidManifest文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hellogxp.sharedpreference3"> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".LoginActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity"></activity> <receiver android:name=".ForceOfflineReceiver"> <intent-filter> <action android:name="com.example.hellogxp.sharedpreference3.FORECE_OFFLINE" /> </intent-filter> </receiver> </application> </manifest>
登录布局文件
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:stretchColumns="1" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow> <TextView android:text="@string/account" android:layout_height="wrap_content" /> <EditText android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/account_hint" android:id="@+id/account" /> </TableRow> <TableRow> <TextView android:text="@string/password" android:layout_height="wrap_content" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:id="@+id/password" /> </TableRow> <TableRow> <CheckBox android:id="@+id/remember_me" android:layout_height="wrap_content" /> <TextView android:layout_height="wrap_content" android:text="@string/remember_me" /> </TableRow> <TableRow> <Button android:layout_span="2" android:text="@string/login" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/login" /> </TableRow> </TableLayout>
Main布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/force_offline" android:text="@string/force_offline" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
项目下载地址
http://pan.baidu.com/s/1jH7tn4y