android小技巧实现重新登陆

1.定义活动集合处理类

public class ActivityCollector {
    public 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();
            }
        }
    }
}
2.构建活动基类
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.编写登陆界面
<?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/txtLoginOne"
            android:text="admin"
            android:layout_height="wrap_content"
            android:hint="输入账户"/>
    </TableRow>
    <TableRow>
        <TextView
            android:layout_height="wrap_content"
            android:text="密码"/>
        <EditText
            android:id="@+id/txtLoginTwo"
            android:text="123456"
            android:layout_height="wrap_content"
            android:inputType="textPassword"/>
    </TableRow>
    <TableRow>
        <Button
            android:id="@+id/btnLoginOne"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="登陆"/>
    </TableRow>
</TableLayout>
4.编写登陆后台代码
public class LoginActivity extends Activity {
    private EditText accountEdite;
    private EditText passwordEdit;
    private Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        accountEdite=(EditText)findViewById(R.id.txtLoginOne);
        passwordEdit=(EditText)findViewById(R.id.txtLoginTwo);
        login=(Button)findViewById(R.id.btnLoginOne);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account=accountEdite.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,"用户或者密码出错",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
 
5.添加消息触发事件
        Button btnBRCTestTwo = (Button) findViewById(R.id.btnBRCTestTwo);
        btnBRCTestTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.LALALAL");
                sendBroadcast(intent);
            }
        } );
6.添加接收消息出来类
public class ForceOfflineReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent)
    {
        AlertDialog.Builder dialogBuilder=new AlertDialog.Builder(context);
        dialogBuilder.setTitle("警告");
        dialogBuilder.setMessage("你将强制重启,请重新登陆。");
        dialogBuilder.setCancelable(false);//不允许回退
        dialogBuilder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityCollector.finishAll();//销毁所有活动
                Intent intent=new Intent(context, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);//因为在广播接收器中启动活动,所以要加上这句
                context.startActivity(intent);//重新启动
            }
        });
        AlertDialog alertDialog=dialogBuilder.create();
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//设置对话框类型,保证广播接收器正常弹出,因为是在广播接收其中弹出的
        alertDialog.show();
    }
}
 
修改配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.zhb.test2" >
    <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" >
        <receiver android:name="com.example.zhb.Com.ForceOfflineReceiver">
            <intent-filter>
                <action android:name="com.example.LALALAL"/>
            </intent-filter>
        </receiver>

        <activity
            android:name="com.example.zhb.Brocast.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>    
    </application>

</manifest>
posted @ 2015-03-17 11:21  yu_liantao  阅读(455)  评论(0编辑  收藏  举报