您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。