怎么在Android项目中添加一个强制下线通知功能
短信预约 -IT技能 免费直播动态提醒
这篇文章将为大家详细讲解有关怎么在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;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项目中添加一个强制下线通知功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341