返回顶部
首页 > 资讯 > 移动开发 >Android模拟强制下线通知功能实例代码
  • 358
分享到

Android模拟强制下线通知功能实例代码

Android 2022-06-06 11:06:21 358人浏览 薄情痞子
摘要

package com.itheima74.broadcastbestpractice; import Android.content.Intent; import a

这里写图片描述
这里写图片描述


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;

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;

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;

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模拟强制下线通知功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程网网站的支持!

您可能感兴趣的文章:Android强制下线功能实现的代码示例Android使用广播(BroadCast)实现强制下线的方法Android运用BroadcastReceiver实现强制下线


--结束END--

本文标题: Android模拟强制下线通知功能实例代码

本文链接: https://lsjlt.com/news/28313.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作