登入页面之强制下线

来源:互联网 发布:程序员基本算法 编辑:程序博客网 时间:2024/04/29 21:08
package com.example.eleven;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * @author HD * @date 2015-12-7 * @package_name com.example.eleven * @file_name MainActivity.java */public class MainActivity extends Activity implements OnClickListener{    private Button btnForce_offline;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        // TODO 自动生成的方法存根        btnForce_offline = (Button) findViewById(R.id.btnForce_offline);        btnForce_offline.setOnClickListener(this);    }    @Override    public void onClick(View v) {        // TODO 自动生成的方法存根        Intent intent = new Intent();        intent.setAction("com.example.eleven.FORCE_OFFLINE");        sendBroadcast(intent);    }}
package com.example.eleven;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;/** * @author HD * @date 2015-12-7 * @package_name com.example.eleven * @file_name LoginActivity.java */public class LoginActivity extends BaseActivity implements OnClickListener {    private Button btnLogin;    private EditText etAccount;    private EditText etPassword;    private CheckBox cbRememberPassword;    private SharedPreferences sharedPreferences;    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO 自动生成的方法存根        super.onCreate(savedInstanceState);        setContentView(R.layout.login);        sharedPreferences = getSharedPreferences("PassWord", Context.MODE_PRIVATE);        initView();        if(sharedPreferences.getBoolean("isRemember", false)){            String accout = sharedPreferences.getString("account", null);            String password = sharedPreferences.getString("password", null);            etAccount.setText(accout);            etPassword.setText(password);        }    }    private void initView() {        btnLogin = (Button) findViewById(R.id.btnLogin);        etAccount = (EditText) findViewById(R.id.etAccount);        etPassword = (EditText) findViewById(R.id.etPassword);        cbRememberPassword = (CheckBox) findViewById(R.id.cbRememberPassWord);        btnLogin.setOnClickListener(this);    }    @Override    public void onClick(View v) {        // TODO 自动生成的方法存根        String account = etAccount.getText().toString();        String password = etPassword.getText().toString();        SharedPreferences.Editor editor = sharedPreferences.edit();        if (account.equals("admin") && password.equals("123456")) {            Toast.makeText(getBaseContext(), "Logining success",                    Toast.LENGTH_SHORT).show();            if (cbRememberPassword.isChecked()) {                editor.putString("account", account);                editor.putString("password", password);                editor.putBoolean("isRemember", true);            }else{                editor.clear();            }            editor.commit();            Intent intent = new Intent();            intent.setClass(LoginActivity.this, MainActivity.class);            startActivity(intent);            finish();        } else {            Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_SHORT)                    .show();        }    }}
package com.example.eleven;import android.app.AlertDialog;import android.content.BroadcastReceiver;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.view.WindowManager;/** * @author HD * @date 2015-12-7 * @package_name com.example.eleven * @file_name ForceOfflineBroadReceiver.java */public class ForceOfflineBroadReceiver extends BroadcastReceiver {    @Override    public void onReceive(final Context context, Intent intent) {        // TODO 自动生成的方法存根        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);        dialogBuilder.setTitle("强制下线");        dialogBuilder.setMessage("因为您的账号在别处登入,所以被系统强制下线。");        dialogBuilder.setIcon(R.drawable.ic_launcher);        dialogBuilder.setCancelable(false);        dialogBuilder.setPositiveButton("确定", new OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                // TODO 自动生成的方法存根                ActivityCollector.finishAll();                Intent intent = new Intent();                intent.setClass(context, LoginActivity.class);                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                context.startActivity(intent);            }        });        AlertDialog alerDialog = dialogBuilder.create();        alerDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);        alerDialog.show();    }}
package com.example.eleven;import android.app.Activity;import android.os.Bundle;/** * @author HD * @date 2015-12-7 * @package_name com.example.eleven * @file_name BaseActivity.java */public class BaseActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO 自动生成的方法存根        super.onCreate(savedInstanceState);        ActivityCollector.addActivity(this);    }    @Override    protected void onDestroy() {        // TODO 自动生成的方法存根        super.onDestroy();        ActivityCollector.removeActivity(this);    }}
package com.example.eleven;import java.util.ArrayList;import java.util.List;import android.app.Activity;/** * @author HD * @date 2015-12-7 * @package_name com.example.eleven * @file_name ActivityCollector.java */public class ActivityCollector {    public static List<Activity> list = new ArrayList<Activity>();    public static void addActivity(Activity activity){        list.add(activity);    }     public static void removeActivity(Activity activity){        list.remove(activity);    }    public static void finishAll(){        for(Activity activity : list){            if(!activity.isFinishing()){                activity.finish();            }        }    }}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.eleven.MainActivity" >    <Button         android:id="@+id/btnForce_offline"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="强制下线"/></RelativeLayout>
<?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"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <TextView            android:id="@+id/tvAccount"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="账号" />        <EditText            android:id="@+id/etAccount"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="4"            android:ellipsize="end"            android:hint="pelase input your account number"            android:singleLine="true" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <TextView            android:id="@+id/tvPassword"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="密码" />        <EditText            android:id="@+id/etPassword"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="4"            android:ellipsize="end"            android:hint="pelase input your password"            android:inputType="textPassword"            android:singleLine="true" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <CheckBox            android:id="@+id/cbRememberPassWord"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:checked="false"            android:text="记住密码" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <Button            android:id="@+id/btnLogin"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:text="登入" />        <Button            android:id="@+id/btnFindPassword"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:text="找回密码" />    </LinearLayout></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.eleven"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="21" />    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".LoginActivity"            android:label="@string/app_name" >            <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=".ForceOfflineBroadReceiver" >            <intent-filter>                <action android:name="com.example.eleven.FORCE_OFFLINE" />            </intent-filter>        </receiver>    </application></manifest>
0 0