安卓强制下线

安卓强制下线,第1张

概述强制下线功能比较常见,很多应用程序都具备这个功能。例如你的QQ号在别处登录了,就会将你强制挤下线。其实实现强行挤下线的思路很简单,只需要在界面上d出一个对话框,让用户无法进行其他 *** 作。必须要点到对话框中的确定按钮,然后回到登录界面即可。1.强制下线功能需要先关闭所有的活动

强制下线功能比较常见,很多应用程序都具备这个功能。例如你的QQ号在别处登录了,就会将你强制挤下线。其实实现强行挤下线的思路很简单,只需要在界面上d出一个对话框,让用户无法进行其他 *** 作。必须要点到对话框中的确定按钮,然后回到登录界面即可。

1.强制下线功能需要先关闭所有的活动,然后回到登录界面,此时我们先建一个ActivityCollector类用于管理所有活动的功能:

 1 package com.example.myapplication; 2  3 import androID.app.Activity; 4  5 import java.util.ArrayList; 6 import java.util.List; 7  8 public class ActivityCollector { 9     public static List<Activity> activitys=new ArrayList<>();//存放活动的集合10     //添加活动11     public static voID addActivity(Activity activity){12         activitys.add(activity);13     }14     //移除活动15     public static voID remove(Activity activity){16         activitys.remove(activity);17     }18     //关闭所有活动19     public static voID finshAll(){20         for(Activity activity:activitys){21             if(!activity.isFinishing()){22                 activity.finish();23             }24         }25         activitys.clear();26     }27 }
VIEw Code

2.然后创建一个BaseActivity类作为所有活动的父类,此父类会动态注册一个广播接收器:

 1 package com.example.myapplication; 2  3 import androID.content.broadcastReceiver; 4 import androID.content.Componentname; 5 import androID.content.Context; 6 import androID.content.DialogInterface; 7 import androID.content.Intent; 8 import androID.content.IntentFilter; 9 import androID.os.Bundle;10 import androID.vIEw.WindowManager;11 12 import androIDx.annotation.Nullable;13 import androIDx.appcompat.app.AlertDialog;14 import androIDx.appcompat.app.AppCompatActivity;15 16 public class BaseActivity extends AppCompatActivity {17     private ForceOfflineReceiver receiver;18 19     @OverrIDe20     protected voID onResume() {21         super.onResume();22         //注册广播23         IntentFilter intentFilter=new IntentFilter();24         intentFilter.addAction("com.example.My Application.FORCE_OFFliNE");25         receiver=new ForceOfflineReceiver();26         registerReceiver(receiver,intentFilter);27     }28 29     @OverrIDe30     protected voID onPause() {31         super.onPause();32         if(receiver!=null){33             unregisterReceiver(receiver);34             receiver=null;35         }36     }37 38     @OverrIDe39     protected voID onCreate(@Nullable Bundle savedInstanceState) {40         super.onCreate(savedInstanceState);41         ActivityCollector.addActivity(this);42     }43     @OverrIDe44     protected voID onDestroy() {45         super.onDestroy();46         ActivityCollector.remove(this);47     }48 49     class ForceOfflineReceiver extends broadcastReceiver{50 51         @OverrIDe52         public voID onReceive(final Context context, Intent intent) {53             AlertDialog.Builder builder=new AlertDialog.Builder(context);54             builder.setTitle("警告");55             builder.setMessage("你被迫下线,请再次登录!");56             builder.setCancelable(false);57             builder.setPositivebutton("OK", new DialogInterface.OnClickListener() {58                 @OverrIDe59                 public voID onClick(DialogInterface dialog, int which) {60                     ActivityCollector.finshAll();//销毁所有活动61                     Intent i=new Intent(context,LoginActivity.class);62                     startActivity(i);//重新启动登录界面63                 }64             });65             AlertDialog alertDialog=builder.create();66             alertDialog.show();67 68         }69     }70 }

3.登录界面activity_login.xml

 1 <?xml version="1.0" enCoding="utf-8"?> 2 <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" 3     androID:orIEntation="vertical" 4     androID:layout_wIDth="match_parent" 5     androID:layout_height="match_parent"> 6  7     <linearLayout 8         androID:orIEntation="horizontal" 9         androID:layout_wIDth="match_parent"10         androID:layout_height="60dp">11         <TextVIEw12             androID:layout_wIDth="90dp"13             androID:layout_height="wrap_content"14             androID:layout_gravity="center_vertical"15             androID:textSize="18sp"16             androID:text="用户名:"/>17         <EditText18             androID:ID="@+ID/username"19             androID:layout_wIDth="0dp"20             androID:layout_height="wrap_content"21             androID:layout_weight="1"22             androID:layout_gravity="center_vertical"/>23     </linearLayout>24 25     <linearLayout26         androID:orIEntation="horizontal"27         androID:layout_wIDth="match_parent"28         androID:layout_height="60dp">29         <TextVIEw30             androID:layout_wIDth="90dp"31             androID:layout_height="wrap_content"32             androID:layout_gravity="center_vertical"33             androID:textSize="18sp"34             androID:text="密码:"/>35         <EditText36             androID:ID="@+ID/password"37             androID:layout_wIDth="0dp"38             androID:layout_height="wrap_content"39             androID:layout_weight="1"40             androID:layout_gravity="center_vertical"/>41     </linearLayout>42     <button43         androID:ID="@+ID/login"44         androID:layout_wIDth="match_parent"45         androID:layout_height="60dp"46         androID:text="登录"/>47 48 49 </linearLayout>
VIEw Code

4.修改MainActivity.java,发送下线广播

 1 package com.example.myapplication; 2  3 import androIDx.appcompat.app.AlertDialog; 4 import androIDx.appcompat.app.AppCompatActivity; 5  6 import androID.content.broadcastReceiver; 7 import androID.content.Componentname; 8 import androID.content.Context; 9 import androID.content.DialogInterface;10 import androID.content.Intent;11 import androID.content.IntentFilter;12 import androID.os.Bundle;13 import androID.vIEw.VIEw;14 import androID.vIEw.WindowManager;15 import androID.Widget.button;16 import androID.Widget.Toast;17 18 public class MainActivity extends BaseActivity {19 20     @OverrIDe21     protected voID onCreate(Bundle savedInstanceState) {22         super.onCreate(savedInstanceState);23         setContentVIEw(R.layout.activity_main);24         button forceOffline=(button)findVIEwByID(R.ID.force_offline);25         forceOffline.setonClickListener(new VIEw.OnClickListener() {26             @OverrIDe27             public voID onClick(VIEw v) {28                 Intent intent=new Intent("com.example.My Application.FORCE_OFFliNE");29                 sendbroadcast(intent);30             }31         });32 33     }34 35 }

activity_main.xml代码非常简单,只需要一个发送按钮就行:

 1 <?xml version="1.0" enCoding="utf-8"?> 2 <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" 3     androID:orIEntation="vertical" 4     androID:layout_wIDth="match_parent" 5     androID:layout_height="match_parent"> 6  7     <button 8         androID:ID="@+ID/force_offline" 9         androID:layout_wIDth="match_parent"10         androID:layout_height="wrap_content"11         androID:text="发生强制下线广播"/>12 13 14 </linearLayout>

最后,修改AndroIDManifest.xml,将登录界面设置为主活动

 1 <?xml version="1.0" enCoding="utf-8"?> 2 <manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" 3     package="com.example.myapplication"> 4     <uses-permission androID:name="androID.permission.SYstem_ALERT_WINDOW"/> 5     <application 6         androID:allowBackup="true" 7         androID:icon="@mipmap/ic_launcher" 8         androID:label="@string/app_name" 9         androID:roundIcon="@mipmap/ic_launcher_round"10         androID:supportsRtl="true"11         androID:theme="@style/Apptheme">12         <activity androID:name=".LoginActivity">13             <intent-filter>14                 <action androID:name="androID.intent.action.MAIN" />15 16                 <category androID:name="androID.intent.category.LAUNCHER" />17             </intent-filter>18         </activity>19         <activity androID:name=".MainActivity">20 21         </activity>22     </application>23 24 </manifest>

结果:

 

总结

以上是内存溢出为你收集整理的安卓强制下线全部内容,希望文章能够帮你解决安卓强制下线所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1060874.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存