Android_BroadcastReceiver(三)

原创 2016年05月31日 17:52:36

如果你已经看了前面两个章节Android_BroadcastReceiver(一)和Android_BroadcastReceiver(二),我想你应该对BroadcastReceiver有了一定的了解了。下面就是一个简单的小例子,希望有助你你对广播更好的理解。话不多少,直接上小例子。

实现强制下线的功能
实现的大体步骤:
1、关闭掉所有的活动。
2、回到登录界面。

编写一个控制Activity的控制器。

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

写一个BaseActivity实现将每一个继承该Activity的类,都加入到ArrayList中。

public class BaseActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityController.addActivity(this);
    }

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

接下里我我们将创建LoginActivity和activity_login.xml布局

public class LoginActivity extends BaseActivity {
    private EditText account, password;
    private Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        account = (EditText) findViewById(R.id.account);
        password = (EditText) findViewById(R.id.passWord);
        login = (Button) findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("admin".equals(account.getText().toString()) && "123456".equals(password.getText().toString())) {
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, "请输入正确的账户名和密码!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

一个简单粗暴的登录界面

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    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"
    tools:context="com.example.bootcompletereceiver.LoginActivity"
    android:stretchColumns="1">
    <TableRow>
        <TextView
            android:layout_height="wrap_content"
            android:text="Account:"/>
        <EditText
            android:id="@+id/account"
            android:layout_height="wrap_content"
            android:hint="Input your account"
            />
    </TableRow>
    <TableRow>
        <TextView
            android:layout_height="wrap_content"
            android:text="Password:"/>
        <EditText
            android:id="@+id/passWord"
            android:layout_height="wrap_content"
            android:hint="Input your Passwrod"
            />
    </TableRow>
    <TableRow>
        <Button
            android:id="@+id/login"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="Login"
            />
    </TableRow>
</TableLayout>

创建一个.class文件,继承BroadcastReceiver,重写onReceive()方法。

public class ForceOfflineReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        AlertDialog.Builder builder =new AlertDialog.Builder(context);
        builder.setTitle("Warning");
        builder.setMessage("You are forced to be offline.Pleace try to login again.");
        builder.setCancelable(false);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                ActivityController.removeAllActivity();
                Intent intent1 =new Intent(context,LoginActivity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.show();
    }
}

在AndroidManifest.xml文件中注册该广播

<receiver android:name=".ForceOfflineReceiver">
            <intent-filter>
                <action android:name="com.example.broadcastbestpractice.FORCE_OFFLINE"/>
            </intent-filter>
</receiver>

编写MainActivity.class文件

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

注意点

Demo注意点

版权声明:本文为博主原创文章,未经博主允许不得转载。

android四大组件笔记之BroadcastReceiver(三)

安卓的开发主要是围绕activity、broadcastReceiver、service、ContentProvider四大组件和基于view的子类组成,其中activity、broadcastRec...

Android四大组件之三:BroadcastReceiver总结

在说BroadcastReceiver之前,让我们先来理解一下广播和广播接收器吧~ 一.如何创建一个BroadcastReceiver广播接收器 ①创建一个继承于BroadcastReceive...

Android四大组件之BroadcastReceiver工作原理

1.广播的注册 As we all know,广播的注册也是分两种:动态注册和静态注册,前者是在Activity生命周期中用java代码注册和解除注册,后者是在AndroidManifest文件中。后...

Android BroadcastReceiver

  • 2014年10月23日 11:13
  • 2.16MB
  • 下载

android BroadcastReceiver详细讲解1-自带的广播接受器-拨号、短信、卸载/安装、开机启动

android中自带的广播接受器在上一篇文章中SD卡的监听 下面我继续来介绍安装与卸载的监听 我们先了解android中安装与卸载的action ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android_BroadcastReceiver(三)
举报原因:
原因补充:

(最多只允许输入30个字)