码迷,mamicode.com
首页 > 其他好文 > 详细

实现强制下线或者注销功能

时间:2015-09-09 17:38:00      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

1.管理活动中类

ActivityCollector.java

import android.app.Activity;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zps on 2015/9/6.
 */
public class ActivityCollector {
    public static List<Activity> activityList = new ArrayList<>();

    public static void addActivity(Activity activity) {
        activityList.add(activity);
    }

    public static void removeActivity(Activity activity) {
        activityList.remove(activity);
    }

    public static void finishAll() {
        for (Activity activity : activityList) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }
}

2.所有活动的父类

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by zps on 2015/9/6.
 */
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);
    }
}

3.创建一个登录界面的布局

login.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1">

    <TableRow>

        <TextView
            android:layout_height="wrap_content"
            android:text="帐 号 : " />

        <EditText
            android:id="@+id/account"
            android:layout_height="wrap_content"
            android:hint="请输入帐号" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_height="wrap_content"
            android:text="密 码 : " />

        <EditText
            android:id="@+id/password"
            android:layout_height="wrap_content"
            android:hint="请输入密码" />
    </TableRow>

    <TableRow>

        <CheckBox
            android:id="@+id/remember_password"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_height="wrap_content"
            android:text="记住密码" />
    </TableRow>

    <TableRow>

        <Button
            android:id="@+id/btn_login"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text=" 登 入 " />
    </TableRow>
</TableLayout>

4 . 登入界面的活动LoginActivity继承自BaseActivity

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by zps on 2015/9/6.
 */
public class LoginActivity extends BaseActivity {
    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private CheckBox rememberPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        accountEdit = (EditText) findViewById(R.id.account);
        passwordEdit = (EditText) findViewById(R.id.password);
        rememberPassword = (CheckBox) findViewById(R.id.remember_password);
        login = (Button) findViewById(R.id.btn_login);
        boolean isRemember = preferences.getBoolean("remember_password", false);
        if (isRemember) {
            String account = preferences.getString("account", "");
            String password = preferences.getString("password", "");
            accountEdit.setText(account);
            passwordEdit.setText(password);
            rememberPassword.setChecked(true);

        }
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                if (account.equals("123456") && password.equals("123456")) {
                    editor = preferences.edit();
                    if (rememberPassword.isChecked()) {
                        editor.putBoolean("remember_password", true);
                        editor.putString("account", account);
                        editor.putString("password", password);

                    } else {
                        editor.clear();
                    }
                    editor.commit();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

5. 下线界面

offline.xml

<RelativeLayout 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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="强制下线" />

</RelativeLayout>

下线活动(按钮发送一条广播)

Offline.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //forceoffline 用于强制下线
        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.xuxian2.FORCE_OFFLINE");
                sendBroadcast(intent);
            }
        });
    }
}

6.广播接收器

ForceOfflineReceiver.java

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.WindowManager;

/**
 * Created by zps on 2015/9/6.
 */
public class ForceOfflineReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle("警告");
        dialog.setMessage("你已经强制下线,请重新登入");

        dialog.setCancelable(false);//是否可以以返回按键返回
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityCollector.finishAll();
                Intent intent = new Intent(context, LoginActivity.class);
                //注意:startActivity(intent);无效
                //加载在栈的启动活动的标志,通过context获取上下文地洞活动
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
        AlertDialog alertDialog = dialog.create();
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.show();
    }
}

7.最后在AndroidManifest.xml文件中配置

<receiver android:name=".ForceOfflineReceiver">
            <intent-filter >
                <action android:name="com.example.xuxian2.FORCE_OFFLINE"/>
            </intent-filter>
</receiver>

实现强制下线或者注销功能

标签:

原文地址:http://www.cnblogs.com/520-1314/p/4795174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!