广播实践--强制用户下线功能



关于广播的基础知识参看我的这篇文章http://blog.csdn.net/jdfkldjlkjdl/article/details/43017091

下面是一个强制用户线下的例子。仅供参考。


强制下线功能需要先关闭掉所有的活动,然后回到登陆界面。

1.首先创建一个项目BroadcastBestPractice,并新建ActivityController类,

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.broadcastbestpractice;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7.   
  8. /**  
  9. * @ClassName: ActivityController  
  10. * @Description: 该类用于管理所有的活动 
  11. * @author xzy  
  12. * @date 2015-1-22 上午9:12:48  
  13. *   
  14. */  
  15. public class ActivityController {  
  16.       
  17.     public static List<Activity> activities = new ArrayList<Activity>();  
  18.     public static void addActivity(Activity activity){  
  19.         activities.add(activity);  
  20.     }  
  21.       
  22.     public static void removeActivity(Activity activity){  
  23.         activities.remove(activity);  
  24.     }  
  25.       
  26.     public static void finishAll(){  
  27.         for(Activity activity:activities){  
  28.             if(!activity.isFinishing()){  
  29.                 activity.finish();  
  30.             }  
  31.         }  
  32.     }  
  33. }  

2.创建BaseActivity作为所有活动的父类。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.broadcastbestpractice;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. /**  
  7. * @ClassName: BaseActivity  
  8. * @Description: 构建所有活动的父类 
  9. * @author xzy  
  10. * @date 2015-1-22 上午9:18:18  
  11. *   
  12. */  
  13. public class BaseActivity extends Activity {  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         // TODO Auto-generated method stub  
  18.         super.onCreate(savedInstanceState);  
  19.         ActivityController.addActivity(this);  
  20.     }  
  21.       
  22.     @Override  
  23.     protected void onDestroy() {  
  24.         // TODO Auto-generated method stub  
  25.         super.onDestroy();  
  26.         ActivityController.removeActivity(this);  
  27.     }  
  28. }  

3.创建一个登陆界面的布局文件login.xml,此处主要实现功能,界面很简单。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:stretchColumns="1" >  
  6.   
  7.     <TableRow>  
  8.   
  9.         <TextView  
  10.             android:layout_height="wrap_content"  
  11.             android:text="Account:" />  
  12.   
  13.         <EditText  
  14.             android:id="@+id/accountEdit"  
  15.             android:layout_height="wrap_content"  
  16.             android:hint="input your account" />  
  17.     </TableRow>  
  18.   
  19.     <TableRow>  
  20.   
  21.         <TextView  
  22.             android:layout_height="wrap_content"  
  23.             android:text="Password:" />  
  24.   
  25.         <EditText  
  26.             android:id="@+id/passwordEdit"  
  27.             android:layout_height="wrap_content"  
  28.             android:inputType="textPassword" />  
  29.     </TableRow>  
  30.   
  31.     <TableRow>  
  32.   
  33.         <Button   
  34.             android:id="@+id/login"  
  35.             android:layout_height="wrap_content"  
  36.             android:layout_span="2"   
  37.             android:text="Login"  
  38.             />  
  39.     </TableRow>  
  40.   
  41. </TableLayout>  


4.写登陆活动 LoginActivity.java,并让其继承自BaseActivity.java.

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.broadcastbestpractice;  
  2.   
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.Toast;  
  10.   
  11. /**  
  12. * @ClassName: LoginActivity  
  13. * @Description: 登陆活动 
  14. * @author xzy  
  15. * @date 2015-1-22 上午9:37:44  
  16. *   
  17. */  
  18. public final class LoginActivity extends BaseActivity {  
  19.       
  20.     private EditText etAccount,etPass;  
  21.     private Button btnLogin;  
  22.       
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         // TODO Auto-generated method stub  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.login);  
  28.         etAccount = (EditText)findViewById(R.id.accountEdit);  
  29.         etPass = (EditText)findViewById(R.id.passwordEdit);  
  30.         btnLogin = (Button)findViewById(R.id.login);  
  31.           
  32.         btnLogin.setOnClickListener(new OnClickListener() {  
  33.               
  34.             @Override  
  35.             public void onClick(View arg0) {  
  36.                 // TODO Auto-generated method stub  
  37.                 String account =  etAccount.getText().toString();  
  38.                 String password = etPass.getText().toString();  
  39.                 //模拟登陆  
  40.                 if(account.equals("xu") && password.equals("123")){  
  41.                     Intent intent = new Intent(LoginActivity.this,MainActivity.class);  
  42.                     startActivity(intent);  
  43.                     finish();  
  44.                 }else{  
  45.                     Toast.makeText(getApplicationContext(), "login failure", Toast.LENGTH_LONG).show();  
  46.                 }  
  47.             }  
  48.         });  
  49.     }  
  50. }  

上提到的MainActivity可以理解成程序登陆成功后进入的程序主界面,此处我们不需要很花哨的功能,只需要在MainActivity中添加强制下线功能即可。故修改activity_main.xml在其中添加一个按钮

5.修改activity_main.xml文件

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <Button  
  12.         android:id="@+id/force_offline"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="Send force offline broadcast" />  
  16.   
  17. </RelativeLayout>  

6.修改MainActivity代码,在该代码中实现强制下线功能

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.broadcastbestpractice;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. /**  
  12. * @ClassName: MainActivity  
  13. * @Description: 强制下线活动 
  14. * @author xzy  
  15. * @date 2015-1-22 上午9:52:30  
  16. *   
  17. */  
  18. public class MainActivity extends Activity {  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.         Button btnForceOffline = (Button)findViewById(R.id.force_offline);  
  25.         btnForceOffline.setOnClickListener(new OnClickListener() {  
  26.               
  27.             @Override  
  28.             public void onClick(View arg0) {  
  29.                 // TODO Auto-generated method stub  
  30.                 //动态注册广播--需要创建一个广播接收器来接收该广播  
  31.                 Intent intent = new Intent("com.example.broadcastbestpractice.FORCE_OFFLINE");  
  32.                 sendBroadcast(intent);  
  33.                   
  34.             }  
  35.         });  
  36.     }  
  37.   
  38.     @Override  
  39.     public boolean onCreateOptionsMenu(Menu menu) {  
  40.         // Inflate the menu; this adds items to the action bar if it is present.  
  41.         getMenuInflater().inflate(R.menu.main, menu);  
  42.         return true;  
  43.     }  
  44.   
  45. }<strong>  
  46. </strong>  
上面的程序很简单,但是有一个重点,就是我们在按钮里面发送了一条广播,广播的值为com.example.broadcastbestpractice.FORCE_OFFLINE;这条广播是用于通知程序强制用户下线的。也就是说强制用户下线程序并不是卸载MainActivity中,而是应该写在接收这条广播的接收器里面,这样强制下线的功能就不会依附于任何界面,不管在程序的那个界面,只要发送一条这样的广播,就可以完成强制下线的操作了。name毫无疑问,接下来我们就需要创建一个广播接收器了。

7.新建接收器ForceOfflineReceiver,继承自BroadcastReceiver

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.broadcastbestpractice;  
  2.   
  3. import android.app.AlertDialog;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.DialogInterface;  
  7. import android.content.Intent;  
  8. import android.view.WindowManager;  
  9.   
  10. /**  
  11. * @ClassName: ForceOfflineReceiver  
  12. * @Description: 广播接收器 
  13. * @author xzy  
  14. * @date 2015-1-22 上午9:57:59  
  15. *   
  16. */  
  17. public class ForceOfflineReceiver extends BroadcastReceiver {  
  18.   
  19.     @Override  
  20.     public void onReceive(final Context context, Intent intent) {  
  21.         // TODO Auto-generated method stub  
  22.         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);  
  23.         dialogBuilder.setTitle("Warning");  
  24.         dialogBuilder.setMessage("You are forced to be offline.Please try to login again.");  
  25.         dialogBuilder.setCancelable(false);  
  26.         dialogBuilder.setPositiveButton("OK"new DialogInterface.OnClickListener() {  
  27.               
  28.             @Override  
  29.             public void onClick(DialogInterface dialog, int which) {  
  30.                 // TODO Auto-generated method stub  
  31.                 //销毁所有的活动  
  32.                 ActivityController.finishAll();  
  33.                 Intent intent = new Intent(context,LoginActivity.class);  
  34.                 //在广播接收器里面启动活动,因此一定要给intent加入如下标识。  
  35.                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  36.                 context.startActivity(intent);  
  37.             }  
  38.         });  
  39.         AlertDialog alertDialog = dialogBuilder.create();  
  40.         //需要设置AlertDialog的类型,保证在广播接收器中可以正常弹出  
  41.         alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);  
  42.         alertDialog.show();  
  43.     }  
  44.   
  45. }<strong>  
  46. </strong>  
这次onReceive()方法里可不再是仅仅弹出一个Toast,而是加入较多的代码,首先使用AlertDialog.Builder来构建一个对话框,注意这里一定要调用setCancelable()方法将对话框设置为不可取消,否则用户按一下back键就可以关闭对话框继续使用程序了。然后使用setPositiveButton()方法来给对话框注册确定按钮,当用户点击了确定按钮时,就调用ActivityController的finishAll()方法来销毁所有的活动,并重新启动LoginActivity这个活动,另外,由于我们在广播接收器里面启动活动的,因为一定要给Intent加入FLAG_ACTIVITY_NEW_TASK这个标志。最后还要把对话框的类型设为TYPE_SYSTEM_ALERT,不然它将无法在广播接收器里弹出。这样的话,所有强制下线的逻辑就已经基本完成,接下来我们还需要对AndroidManifest.xml文件进行配置。


8.配置AndroidManifest.xml文件

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.broadcastbestpractice"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="18" />  
  10.     <span style="color:#cc0000;"><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/></span>  
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.example.broadcastbestpractice.LoginActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.          <activity  
  25.             android:name="com.example.broadcastbestpractice.MainActivity" >        
  26.         </activity>  
  27.        <span style="color:#cc0000;"> <receiver android:name=".ForceOfflineReceiver">  
  28.             <intent-filter>  
  29.                 <action android:name="com.example.broadcastbestpractice.FORCE_OFFLINE" />  
  30.                 </intent-filter>  
  31.             </receiver></span>  
  32.     </application>  
  33.   
  34. </manifest>  

上面的配置文件需要注意的几点:1.由于在ForceOfflineReceiver里弹出了一个系统级别的对话框,因此必须要声明android.permission.SYSTEM_ALERT_WINDOW权限,然后对LoginActivity进行注册,并把它设置为主活动,然后再对ForceOfflineReceiver进行注册,并指定它接收broadcastbestpractice.FORCE_OFFLINE这条广播。


程序源代码:http://pan.baidu.com/s/1i3w0f57


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值