package com.itheima74.broadcastbestpractice;import android.content.Intent;import android.os.Bundle;import android.os.SystemClock;import android.support.v4.content.LocalBroadcastManager;import android.view.View;/** * 模拟强制下线通知 * 1.登录成功后10秒发送一条本地自定义无序广播 * 2.接收到广播后弹出对话框(不可取消) * 3.点击对话框确定按钮重新返回登陆界面 */public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 启动一个子线程,10秒发送一条本地无序广播 new Thread(new Runnable() { @Override public void run() { SystemClock.sleep(1000 * 10); LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext()); localBroadcastManager.sendBroadcast(new Intent("com.itheima74.broadcastbestpractice.FORCE_OFFLINE")); } }).start(); } // 点击按钮,启动Main2Activity public void click(View view) { startActivity(new Intent(this, Main2Activity.class)); }}
package com.itheima74.broadcastbestpractice;import android.content.Intent;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.EditText;import android.widget.Toast;/** * Created by My on 2017/3/6. */public class LoginActivity extends AppCompatActivity { private EditText et_username; private EditText et_password; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); et_username = (EditText) findViewById(R.id.et_username); et_password = (EditText) findViewById(R.id.et_password); } // 点击按钮,进行登录 public void login(View view) { String username = et_username.getText().toString().trim(); String password = et_password.getText().toString().trim(); if ("admin".equals(username) && "123456".equals(password)) { Toast.makeText(this, "login success", Toast.LENGTH_SHORT).show(); startActivity(new Intent(this, MainActivity.class)); } else { Toast.makeText(this, "login fail", Toast.LENGTH_SHORT).show(); } }}
package com.itheima74.broadcastbestpractice;import android.app.AlertDialog;import android.content.BroadcastReceiver;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.content.LocalBroadcastManager;import android.support.v7.app.AppCompatActivity;import android.util.Log;import java.util.ArrayList;/** * BaseActivity * 1.记录所有新创建的Activity * 2.随时销毁所有Activity * 3.所有子类获取焦点注册广播接收者,失去焦点取消注册 * 4.收到强制下线广播,弹出对话框,结束所有Activity,重新启动LoginActivity * Created by My on 2017/3/6. */public class BaseActivity extends AppCompatActivity { protected static ArrayList<BaseActivity> activityList = new ArrayList<>(); private LocalBroadcastManager localBroadcastManager; private OffLineReceiver offLineReceiver; private IntentFilter intentFilter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); activityList.add(this); Log.d("china", getClass().getSimpleName() + " onCreate activityList.size():" + activityList.size()); localBroadcastManager = LocalBroadcastManager.getInstance(this); offLineReceiver = new OffLineReceiver(); intentFilter = new IntentFilter("com.itheima74.broadcastbestpractice.FORCE_OFFLINE"); } @Override protected void onDestroy() { super.onDestroy(); activityList.remove(this); Log.d("china", getClass().getSimpleName() + " onDestroy activityList.size():" + activityList.size()); } // 获取焦点,注册广播接收者 @Override protected void onResume() { super.onResume(); localBroadcastManager.registerReceiver(offLineReceiver, intentFilter); } // 失去焦点,取消注册广播接收者 @Override protected void onPause() { super.onPause(); localBroadcastManager.unregisterReceiver(offLineReceiver); } protected class OffLineReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { // 弹出对话框 AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this); builder.setTitle("Warning"); builder.setMessage("You are force to be offline,Please try to login again."); builder.setCancelable(false);// 不允许back键取消 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { BaseActivity.finishAll(); // 重新启动LoginActivity,广播接收者中需要添加任务栈 Intent intent = new Intent(context, LoginActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } }); builder.show(); } } public static void finishAll() { for (BaseActivity baseActivity : activityList) { if (!baseActivity.isFinishing()) { baseActivity.finish(); } } }}
以上所述是小编给大家介绍的Android模拟强制下线通知功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!
新闻热点
疑难解答