参考:《第一行代码》
效果
创建ActivityController类用于管理所有的活动
public class ActivityController {
public static ArrayList<Activity> activityList=new ArrayList<Activity>();
public static void add(Activity activity)
{
activityList.add(activity);
}
public static void remove(Activity activity)
{
activityList.remove(activity);
}
public static void finishAll()
{
for(Activity a:activityList)
{
if(!a.isFinishing())
{
a.finish();
}
}
}
}
创建BaseActivity类作为所有活动的父类
public class BaseActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ActivityController.add(this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
ActivityController.remove(this);
}
}
登录布局
<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="Account" />
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="Input your account" />
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:hint="Input your password" />
</TableRow>
<TableRow >
<Button
android:layout_span="2"
android:id="@+id/login"
android:layout_height="wrap_content"
android:text="Login"/>
</TableRow>
</TableLayout>
创建登录LoginActivity类,继承BaseActivity类
public class LoginActivity extends BaseActivity{
private EditText accountEdit;
private EditText passwordEdit;
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
init();
myListener();
}
public void init()
{
accountEdit=(EditText) findViewById(R.id.account);
passwordEdit=(EditText) findViewById(R.id.password);
login=(Button) findViewById(R.id.login);
}
private void myListener() {
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String account=accountEdit.getText().toString();
String password=passwordEdit.getText().toString();
if(account.equals("csdn")&&password.equals("123"))
{
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
//关掉当前活动
finish();
}else{
Toast.makeText(LoginActivity.this,
"账号或者密码输入不正确,请再次输入!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
主页面布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.Controller.MainActivity"
tools:ignore="MergeRootFrame">
<Button
android:id="@+id/force_offline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="send force offline broadcast"
></Button>
</FrameLayout>
MainActivity 类
点击按钮后会发个广播
public class MainActivity extends BaseActivity {
Button forceOffline;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
forceOffline=(Button) findViewById(R.id.force_offline);
myListener();
}
private void myListener() {
forceOffline.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent("com.example.Controller.FORCE_OFFLINE");
sendBroadcast(intent);
}
});
}
}
广播接收者
接收到MainActivity 发来的广播后,创建对话框,实现强制下线功能
public class ForOfflineReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
// 创建一个对话框,用于提醒用户强制下线,然后确定退出到登录界面
/*
* 不能直接通过AlertDialog的构造函数来生产一个AlertDialog。
* 研究AlertDialog的源码发现AlertDialog所有的构造方法都是写保护的
*/
AlertDialog.Builder alertDialogBuilder=new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Warning");
alertDialogBuilder.setMessage("You are forced to offline");
alertDialogBuilder.setCancelable(false);//设置不能返回
alertDialogBuilder.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//销毁所有活动
ActivityController.finishAll();
Intent intent=new Intent(context,LoginActivity.class);
//根据配置的Affinity决定创建的活动在哪个栈
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//因为本身不是Activity,使用上下文来启动另一个活动
context.startActivity(intent);
}
});
AlertDialog alertDialog=alertDialogBuilder.create();
//需要设置AlertDialog的类型,保护在广播接收器中可以正常弹出, 系统提示。它总是出现在应用程序窗口之上
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
}
}
配置
加上
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
否则对话框无法在广播接收器弹出
还有广播接收器是静态注册,所以不需程序启动即可接收,完整配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Controller"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<!-- allowBackup="true"表示允许备份的意思,涉及设置存在敏感信息泄露 -->
<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" />
<receiver android:name=".ForOfflineReceiver" >
<intent-filter>
<action android:name="com.example.Controller.FORCE_OFFLINE" />
</intent-filter>
</receiver>
</application>
</manifest>