Android(Broadcast实现强制下线功能)

1、效果图:
这里写图片描述
这里写图片描述
2、文件:
这里写图片描述
3、ActivityCollector.java

package com.example.jcs.mybroadcastreceiverdemo;

import android.app.Activity;

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

//定义一个类来管理所有活动
public class ActivityCollector {

    public static List<Activity> list = new ArrayList<Activity>();

    public static void addActivity(Activity activity) {
        list.add(activity);
    }

    public static void removeActivity(Activity activity) {
        list.remove(activity);
    }

    public static void finishAll() {
        for (Activity a : list) {
            //遍历集合是否到最后一个
            if (!a.isFinishing()) {
                a.finish();
            }
        }

    }

}

4、Base.java

package com.example.jcs.mybroadcastreceiverdemo;

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;

public class Base extends AppCompatActivity {

    private Receiver 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.jcs.mybroadcastreceiverdemo.FORCE_OFFLINE");
        receiver = new Receiver();
        registerReceiver(receiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //判断对象是否为空
        if (receiver != null) {
            unregisterReceiver(receiver);
            receiver = null;
        }

    }

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

    }

    //增加一个广播接收器
    class Receiver extends BroadcastReceiver {

        @Override
        public void onReceive(final Context context, Intent intent) {

            //对话框
            AlertDialog.Builder dialog = new AlertDialog.Builder(context);
            dialog.setTitle("警告");
            dialog.setMessage("应用强制下线!");
            dialog.setCancelable(false);
            dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //移除所有的页面
                    ActivityCollector.finishAll();
                    //在转到login
                    Intent intent = new Intent(context,Login.class);
                    context.startActivity(intent);
                }
            });
            //PS:一定要show();
            dialog.show();
        }
    }
}

5、Login.java

package com.example.jcs.mybroadcastreceiverdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by JCS on 2017/6/27.
 */
public class Login extends Base {
    private EditText user_name;
    private EditText password;
    private Button btn;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        user_name = (EditText) findViewById(R.id.user);
        password = (EditText) findViewById(R.id.password);
        btn = (Button) findViewById(R.id.login_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String name = user_name.getText().toString().trim();
                String pass = password.getText().toString().trim();
                //判断账号密码
                if (name.equals("admin") && pass.equals("123")) {
                    Intent intent = new Intent(Login.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(Login.this, "error", Toast.LENGTH_SHORT).show();
                }


            }
        });
    }
}

6、MainActivity.java

package com.example.jcs.mybroadcastreceiverdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Base {
    private Button btn;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.send_broadcast);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击就发送广播
                Intent intent = new Intent("com.example.jcs.mybroadcastreceiverdemo.FORCE_OFFLINE");
                sendBroadcast(intent);
            }
        });


    }
}

7、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: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.jcs.mybroadcastreceiverdemo.MainActivity">

  <Button
      android:id="@+id/send_broadcast"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="发送广播"
      />
</RelativeLayout>

8、login_activity.xml

<?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:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            />
        <EditText
            android:id="@+id/user"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            />
        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
    <Button
        android:id="@+id/login_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        />
</LinearLayout>

9、清单文件:(把login为启动页面)

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"></activity>
        <activity android:name=".Login">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值