1. 程式人生 > >廣播的最佳實踐——實現強制下線功能

廣播的最佳實踐——實現強制下線功能

類ActivityCollector

package com.example.broadcastbestpractice;

import android.app.Activity;

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

/**
 * Created by 123 on 2018/10/18.
 */

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();
            }
        }
    }
}

類BaseAvtivity

package com.example.broadcastbestpractice;

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.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

/**
 * Created by 123 on 2018/10/18.
 */

public class BaseActivity extends AppCompatActivity{
    private  ForceOfflineReceiver receiver;
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onResume(){
        super.onResume();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("com.example.broadcastbestpractice.FORCE_OFFLINE");
        receiver =new ForceOfflineReceiver();
        registerReceiver(receiver,intentFilter);

    }
    protected void onPause(){
        super.onPause();
        if(receiver!=null){
            unregisterReceiver(receiver);
            receiver=null;
        }
    }

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

    class ForceOfflineReceiver extends BroadcastReceiver{
        public  void onReceive(final Context context, final Intent intent)
        {
            AlertDialog.Builder builder=new AlertDialog.Builder(context);
            builder.setTitle("Warning");
            builder.setMessage("login again");
            builder.setCancelable(false);
            builder.setPositiveButton("OK",new DialogInterface.OnClickListener()
            {
                public  void onClick(DialogInterface dialog,int which)
                {
                    ActivityCollector.finishAll();
                    Intent intent1= new Intent(context,LoginActivity.class);
                    context.startActivity(intent);
                }
            });
            builder.show();
        }
    }



}

類LoginActivity

package com.example.broadcastbestpractice;

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

public class LoginActivity extends BaseActivity{
    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;
    protected void onCreate(Bundle saveInstanceState)
    {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.activity_login);
        accountEdit =(EditText)findViewById(R.id.account);
        passwordEdit=(EditText)findViewById(R.id.password);
        login=(Button)findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String account =accountEdit.getText().toString();
                String password=passwordEdit.getText().toString();
             if (account.equals("admin")&&password.equals("123456"))
             {
                 Intent intent =new Intent(LoginActivity.this,MainActivity.class);
                 startActivity(intent);
                 finish();
            }
                else
             {
                 Toast.makeText(LoginActivity.this,"account or password is invalid",Toast.LENGTH_SHORT).show();
             }


             }

        });
    }

}

類MainACtivity

package com.example.broadcastbestpractice;

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

import java.sql.BatchUpdateException;

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button forceoffline=(Button)findViewById(R.id.force_offline);
        forceoffline.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent=new Intent("com.example.broadcastbestpractice.FORCE_OFFLINE");
                sendBroadcast(intent);
            }
        });
    }
}

activity_login.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical"
    >

    <!-- Login progress -->
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal">
    <TextView
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="18sp"
        android:text="Account:"
        />
     <EditText
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:id="@+id/account"
         android:layout_weight="1"
         android:layout_gravity="center_vertical"/>
      </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="90dp"

            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textSize="18sp"
            android:text="Password"
            />
        <EditText
            android:layout_width="0dp"

            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:id="@+id/password"
            android:inputType="textPassword"/>
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/login"
        android:text="Login"/>
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.broadcastbestpractice.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send force offline broadcast"
        android:id="@+id/force_offline"/>
</RelativeLayout>

Androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.broadcastbestpractice.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send force offline broadcast"
        android:id="@+id/force_offline"/>
</RelativeLayout>

123