您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章将为大家详细讲解有关怎么在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项目中添加一个强制下线通知功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。