欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-04 15:48:19
package com.itheima74.broadcastbestpractice; import android.content.intent;...

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

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模拟强制下线通知功能实例代码,希望对大家有所帮助