Android笔记(二十六)广播实践——实现强制下线功能
2015-09-09 09:55
591 查看
强制下线功能需要先关闭掉所有的活动,然后回到登录界面。先创建一个 ActivityCollector 类用于管理所有的活动,代码如下所示:
然后创建 BaseActivity 类作为所有活动的父类,代码如下所示:
创建登录界面的布局
登录界面活动LoginActivity
跳转界面布局
跳转界面活动
强制下线***
AndroidManifest.xml
注意:当时在LoginActivity中忘了加载布局文件,导致整个应用无错误,但是一打开应用就崩。
setContentView(R.layout.login);
public class ActivityCollector { public static List<Activity> activitys = new ArrayList<Activity>(); public static void addActivity(Activity activity) { activitys.add(activity); } public static void removeActivity(Activity activity) { activitys.remove(activity); } public static void finishAll() { for (Activity activity : activitys) { if (!activity.isFinishing()) { activity.finish(); } } } }
然后创建 BaseActivity 类作为所有活动的父类,代码如下所示:
public class BaseActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); ActivityCollector.addActivity(this); } 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>
登录界面活动LoginActivity
public class LoginActivity extends BaseActivity { private EditText usernameet; private EditText passwordet; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.login); usernameet=(EditText) findViewById(R.id.username); passwordet=(EditText) findViewById(R.id.password); button=(Button) findViewById(R.id.login); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String username=usernameet.getText().toString(); String password=passwordet.getText().toString(); //账号android密码123 if(username.equals("android")&&password.equals("123")){ Intent intent =new Intent(LoginActivity.this,SecondActivity.class); startActivity(intent); finish(); }else{ Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show(); } } }); } }
跳转界面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/force_offline" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send force offline broadcast" /> </LinearLayout>
跳转界面活动
public class SecondActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second); Button button=(Button) findViewById(R.id.force_offline); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent("com.example.another.FORCE_OFFLINE"); sendBroadcast(intent); } }); } }
强制下线***
public class ForceOfflineReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { // TODO Auto-generated method stub AlertDialog.Builder dialog=new AlertDialog.Builder(context); dialog.setTitle("Warning"); dialog.setMessage("此账号已在别处登录,您已被强制下线"); dialog.setCancelable(false); dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub ActivityCollector.finishAll(); Intent intent=new Intent(context,LoginActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } }); AlertDialog alertDialog=dialog.create(); alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); alertDialog.show(); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.another" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <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=".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=".SecondActivity" > </activity> <receiver android:name=".ForceOfflineReceiver" > <intent-filter> <action android:name="com.example.another.FORCE_OFFLINE" /> </intent-filter> </receiver> </application> </manifest>
注意:当时在LoginActivity中忘了加载布局文件,导致整个应用无错误,但是一打开应用就崩。
setContentView(R.layout.login);
相关文章推荐
- Android里ListView里面添加CheckBox实现多选。
- android TraceView (图形化性能测试工具)使用入门笔记
- android Activity.this.getLayoutInflater()::动态加载布局文件,实现弹窗效
- AndroidStudio快捷键
- 【Andorid】Gradle多渠道打包
- Android View或Layout点击变色效果
- Android控件之ProgressBar用法实例分析
- Android 手把手教您自定义ViewGroup(一)
- Android-ViewPagerIndicator框架使用——CirclePageIndicator
- android 使用iconfont
- Android SDK Manager 更新代理配置
- 关于Android内存泄露的说明
- Android的StrictMode
- Android开源项目分类汇总+Android开发工具
- 去除ListView默认点击背景,去除LietView滚动条
- 收集炫酷的Android Loading动画
- android隐藏软键盘的方法
- Android技能学习
- Android百分比布局支持库
- Android listView异步下载和convertView复用产生的错位问题