1. 程式人生 > >《第一行程式碼》實現強制下線功能

《第一行程式碼》實現強制下線功能

第5章的內容不是很多,也很簡單,最後的這個小專案總結一下。

強制下線功能就是類似於QQ的那個,在重複上線時把你頂掉線,我們要做的就是在頁面上點選一下按鈕,彈出來對話方塊,點選對話方塊,就會回到登入介面,不可返回。

這意味著,當點選按鈕的同時,系統把所有的的活動都殺死掉,然後重新啟動登入活動。這裡需要一個管理所有活動的類;那個按鈕肯定是一個廣播發射器,我們需要一個接收器,我們會想那應該每個介面都需要一個接收器啊,重複的程式碼太多了。其實我們可以讓所有的活動繼承自一個Activity的子類,在這個子類中實現接收器的設定和回收。更加準確的來說,我們需要在Activity的活動週期的onResume和onPause裡面分別寫註冊和銷燬【

與前面的內容連上了】,我們只需要在回收棧的頂上聽到廣播就可以了,其他的活動沒必要聽到。

管理類

package com.example.forceunload;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

public class ActivityCollector  {
    public static List<Activity> activities=new ArrayList<>();
    public static void addActivity(Activity   activity)
    {
        activities.add(activity);
    }
    public static void removeActivity(Activity activity)
    {
        activities.remove(activity);
    }
    public static void finishAll()
    {
        for (Activity activity :activities)
        {
            if (!activity.isFinishing())
            {
                activity.finish();
            }
        }
    }
}

//這是登入介面,用來判斷密碼是否正確
package com.example.forceunload;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LoadActivity extends BaseActivity {
    private Button buttonforload;
    private EditText editText_account;
    private EditText editText_pass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load);
        buttonforload=(Button) findViewById(R.id.Login);
        editText_account=(EditText) findViewById(R.id.accout_text);
        editText_pass=(EditText) findViewById(R.id.password_text);
        buttonforload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String accout=editText_account.getText().toString();
                String password=editText_pass.getText().toString();
                if(accout.equals("admin")&&password.equals("123456"))
                {
                    Intent intent=new Intent(LoadActivity.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        });
    }
}

對應的佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
>
    <LinearLayout
    android:id="@+id/line1"
    android:layout_margin="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/account"
        android:text="Account   "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/accout_text"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_marginLeft="5dp"
        android:layout_height="wrap_content" />
</LinearLayout>
    <LinearLayout
        android:id="@+id/line2"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/password"
            android:text="Password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <EditText
            android:id="@+id/password_text"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:inputType="textPassword"
            android:layout_height="wrap_content" />
</LinearLayout>
    <Button
        android:id="@+id/Login"
        android:text="Login"
        android:textAllCaps="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
//這是廣播發送器,也是強制下線按鈕的所在地
package com.example.forceunload;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button) findViewById(R.id.force);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent("com.android.forceunload.FORCR_OFFLINE");
                sendBroadcast(intent);
            }
        });
    }
}

對應的佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >
    <Button
        android:id="@+id/force"
        android:text="下線"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

最重要的父類

package com.example.forceunload;

import android.app.Activity;
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;

public class BaseActivity extends Activity {
    private  Myreceiver myreceiver;
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this);
    }
    class Myreceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(final Context context, Intent intent) {
            AlertDialog.Builder builder=new AlertDialog.Builder(context);
            builder.setTitle("警告");
            builder.setMessage("重複上線,你已被踢下線!");
            builder.setCancelable(false);
            builder.setPositiveButton("好", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ActivityCollector.finishAll();
                    Intent intent=new Intent(context,LoadActivity.class);
                    startActivity(intent);
                }
            });
               builder.show();
            }
        }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("com.android.forceunload.FORCR_OFFLINE");
        myreceiver=new Myreceiver();
        registerReceiver(myreceiver,intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(myreceiver!=null)
        {unregisterReceiver(myreceiver);}
        else
        {
            myreceiver=null;
        }
    }
}