关闭

Android_BroadcastReceiver(三)

标签: androidbroadcastreceiver界面
117人阅读 评论(0) 收藏 举报
分类:

如果你已经看了前面两个章节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注意点

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2914次
    • 积分:203
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档