强制下线功能:先关闭掉所有的活动,然后回到登陆界面
相应工程:http://download.csdn.net/detail/u012005313/9004889
先创建一个ActivityCollector类用于管理所有的活动
package com.example.zj.broadcastbestpractice; import android.app.Activity; import java.util.ArrayList; import java.util.List; /** * Created by zj on 15-8-14. */ 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(); } } } }
创建BaseActivity类作为所有活动的父类
package com.example.zj.broadcastbestpractice; import android.app.Activity; import android.os.Bundle; /** * Created by zj on 15-8-14. */ 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); } }
创建一个登陆界面的布局
<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:inputType="textPassword" /> </TableRow> <TableRow> <Button android:id="@+id/login" android:layout_height="wrap_content" android:layout_span="2" android:text="Login" /> </TableRow> </TableLayout>
package com.example.zj.broadcastbestpractice; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends BaseActivity { private EditText accountEdit; private EditText passwordEdit; private Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); accountEdit = (EditText)findViewById(R.id.account); passwordEdit = (EditText)findViewById(R.id.password); login = (Button)findViewById(R.id.login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String account = accountEdit.getText().toString(); String password = passwordEdit.getText().toString(); if (account.equals("admin") && password.equals("123456")) { Intent intent = new Intent(LoginActivity.this, MainActivity.class); Log.i("this", "------------------------intent to MainActivity"); startActivity(intent); finish(); } else { Toast.makeText(LoginActivity.this, "account or password error", Toast.LENGTH_SHORT).show(); } } }); } }
用户名:admin
密码:123456
MainActivity可理解为登录成功后的程序主界面,在主界面设置一个按钮,增加强制下线的功能
修改activity_main.xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/force_offline" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send force offline broadcast" /> </LinearLayout>
package com.example.zj.broadcastbestpractice; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button forceOffline = (Button)findViewById(R.id.force_offline); forceOffline.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.zj.broadcastbestpractice.FORCE_OFFLINE"); sendBroadcast(intent); } }); } }
com.zj.broadcastbestpractice.FORCE_OFFLINE这条广播就是用于通知程序强制用户下线的,也就是说强制用户下线的逻辑并不是写在MainActivity里面,而是应该写在接受这条广播的广播接收器里面,这样强制下线的功能就不会依附于任何的界面,不管是在程序的任何地方,只需要发出这样一条广播,就可以完成强制下线的操作
创建一个广播接收器,新建ForceOfflineReceiver继承自BroadcastReceiver
package com.example.zj.broadcastbestpractice; 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 root on 15-8-14. */ public class ForceOfflineReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context); dialogBuilder.setTitle("Warning"); dialogBuilder.setMessage("You are forced to be offline. Please try to login again."); dialogBuilder.setCancelable(false); dialogBuilder.setPositiveButton("OK", 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_NEW_TASK); context.startActivity(intent); } }); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); alertDialog.show(); } }
首先使用AlertDialog.Builder来构建一个对话框(调用setCancelable()方法将对话框设为不可取消,否则用户按一下Back键就可以关闭对话框继续使用程序)
然后使用setPositiveButton()方法给对话框注册确定按钮,当用户点击了确定按钮时,就调用ActivityCollector的finishAll()方法来销毁所有的活动,并重新启动LoginActivity
note:由于我们是在广播接收器里启动活动,因此一定要给Intent加入FLAG_ACTIVITY_NEW_TASK这个标志,最后,还需要把对话框的类型设为TYPE_SYSTEM_ALERT,不然无法在广播接收器中弹出
配置AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.zj.broadcastbestpractice" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > </activity> <activity android:name=".LoginActivity" android:label="@string/title_activity_login" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".ForceOfflineReceiver" > <intent-filter> <action android:name="com.zj.broadcastbestpractice.FORCE_OFFLINE" /> </intent-filter> </receiver> </application> </manifest>