本文实例讲述了android使用广播(broadcast)实现强制下线的方法。分享给大家供大家参考,具体如下:
使用广播简单模拟强制下线功能。
1、新建活动管理类
新建activity活动管理类,方便管理所有活动。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.example.broadcastpractice; import java.util.arraylist; import java.util.list; import android.app.activity; /** * 所有活动管理器 * @author yy * */ public class activitycollector { public static list<activity> activities = new arraylist<activity>(); /** * 添加活动 * @param activity */ public static void addactivity(activity activity){ if (!activities.contains(activity)){ activities.add(activity); } } /** * 移除活动 * @param activity */ public static void removeactivity(activity activity){ activities.remove(activity); } /** * 结束所有活动 */ public static void finishall(){ for (activity activity : activities){ if (!activity.isfinishing()){ activity.finish(); } } } } |
2、新建活动基类
用于管理activity。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.example.broadcastpractice; import android.app.activity; import android.os.bundle; /** * 所有活动的基类 * @author yy * */ 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,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<?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 android:id= "@+id/tablerow1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "account:" /> <edittext android:id= "@+id/account" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:hint= "input your account here" android:ems= "10" > </edittext> </tablerow> <tablerow android:id= "@+id/tablerow2" android:layout_width= "wrap_content" android:layout_height= "wrap_content" > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "password:" /> <edittext android:id= "@+id/password" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:ems= "10" android:inputtype= "textpassword" > </edittext> </tablerow> <tablerow android:id= "@+id/tablerow3" android:layout_width= "wrap_content" android:layout_height= "wrap_content" > <button android:id= "@+id/login" android:layout_span= "2" android:layout_height= "wrap_content" android:text= "login" /> </tablerow> </tablelayout> |
使用的是tablelayout,当然也可以使用其他布局,在该布局中,定义了登陆等相关信息。
4、新建登陆布局对应的activity
新建登陆布局对应的activity类,用于加载登陆布局:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package com.example.broadcastpractice; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; 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 loginbutton; @override protected void oncreate(bundle savedinstancestate) { //首先调用父类的方法 super .oncreate(savedinstancestate); //加载登陆布局 setcontentview(r.layout.login); //获取输入内容 accountedit = (edittext) findviewbyid(r.id.account); passwordedit = (edittext) findviewbyid(r.id.password); loginbutton = (button) findviewbyid(r.id.login); //注册按钮点击事件 loginbutton.setonclicklistener( new onclicklistener() { @override public void onclick(view v) { string account = accountedit.gettext().tostring(); string password = passwordedit.gettext().tostring(); if (account.equals( "yy" ) && password.equals( "123456" )){ //登陆成功 //启用mainactivity活动 intent intent = new intent(loginactivity. this ,mainactivity. class ); //启动 startactivity(intent); //当前活动销毁 finish(); } else { //登陆失败 //清空内容 accountedit.settext( "" ); passwordedit.settext( "" ); toast.maketext(loginactivity. this , "登陆失败,请重新登陆!!" , toast.length_short).show(); } } }); } } |
在登陆按钮的监听事件中,当登陆验证通过,新新一个活动mainactivity,同时销毁当前活动,如果登陆失败,则清空所填内容。
5、mainactivity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package com.example.broadcastpractice; import android.content.intent; import android.os.bundle; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; /** * 继承baseactivity * @author yy * */ public class mainactivity extends baseactivity { @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 onclicklistener() { @override public void onclick(view v) { //启用广播 intent intent = new intent( "com.example.broadcastpractice.force_offline" ); //发送广播--标准广播 sendbroadcast(intent); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true ; } } |
下面是mainactivity对应的布局文件activity_main.xml:
1
2
3
4
5
6
7
8
9
10
|
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <button android:id= "@+id/force_offline" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "强制下线" /> </linearlayout> |
即在该布局中有一按钮,当点击时,发送强制下线的广播,当然了我们接下来要创建广播接收器:
6、广播接收器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package com.example.broadcastpractice; import android.app.alertdialog; import android.content.broadcastreceiver; import android.content.context; import android.content.dialoginterface; import android.content.dialoginterface.onclicklistener; import android.content.intent; import android.view.windowmanager; /** * 强制下线广播接收器 * @author yy * */ public class forceofflinebroadcastreceiver extends broadcastreceiver { @override public void onreceive( final context context, intent intent) { //对话框 alertdialog.builder builder = new alertdialog.builder(context); builder.settitle( "警告" ); //正文 builder.setmessage( "你已被强制下线,请重新登陆。" ); //不可取消 builder.setcancelable( false ); //按钮 builder.setpositivebutton( "确定" , new 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 alterdialog = builder.create(); //添加对话框类型:保证在广播中正常弹出 alterdialog.getwindow().settype(windowmanager.layoutparams.type_system_alert); //对话框展示 alterdialog.show(); } } |
7、全集配置文件androidmanifest.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<?xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package = "com.example.broadcastpractice" android:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "14" android:targetsdkversion= "17" /> <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" > <!-- 登陆活动为主活动 --> <activity android:name= "com.example.broadcastpractice.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> <!-- mainactivity活动 --> <activity android:name= ".mainactivity" /> <!-- 广播接收器 --> <receiver android:name= ".forceofflinebroadcastreceiver" > <intent-filter> <action android:name= "com.example.broadcastpractice.force_offline" /> </intent-filter> </receiver> </application> </manifest> |
8、效果展示
当输入错误时,验证不通过,显示上方左图。当输入正确时,验证通过,进入mainactivity界面,点击按钮,显示上方右图。当点击确定时,会跳到登陆界面。
希望本文所述对大家android程序设计有所帮助。