在该例子中,我会实现一个简单的登录功能,登陆成功后点击按钮发送一条强制下线的广播,在广播接收器中进行处理,弹出对话框要求用户强制下线,并跳转到登陆界面。通过该例子主要学习如何发送自定义广播以及如何接收广播并进行处理。
简单的登录界面
<?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"
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="username"
android:textSize="15sp" />
<EditText
android:id="@+id/login_userName_EditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="3"
android:hint="input your useruame" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="password"
android:textSize="15sp" />
<EditText
android:id="@+id/login_password_EditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="3" />
</LinearLayout>
<Button
android:id="@+id/login_loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:text="Login" />
</LinearLayout>
布局很简单

登录的处理,如果用户名="user"密码="123456",就进入SecondActivity,否则Toast一下
public class SecondActivity extends BaseActivity implements OnClickListener{
private Button mLoginButton;
private EditText mUserNameEditText;
private EditText mPasswordEditText;
private static final String TAG="Secondctivity";
@Override
public void findId() {
mLoginButton=(Button) findViewById(R.id.login_loginButton);
mUserNameEditText=(EditText) findViewById(R.id.login_userName_EditText);
mPasswordEditText=(EditText) findViewById(R.id.login_password_EditText);
}
@Override
public void setListener() {
mLoginButton.setOnClickListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login_loginButton:
String userName=mUserNameEditText.getText().toString();
String password=mPasswordEditText.getText().toString();
if(userName.equals("user") && password.equals("123456")){
startActivity(new Intent(SecondActivity.this,ThreeActivity.class));
finish();
}else{
Toast.makeText(SecondActivity.this, "用户名或密码错误", Toast.LENGTH_LONG).show();
}
break;
}
}
}
ThreeActivity只有一个按钮,界面和布局代码都不再给出,该按钮的功能是点击后发出一条强制下线的广播
首先构建一个Intent对象,把要发送的值传进去,然后调用Context的sendBroadcast方法,将广播发出去,这样所有监听com.xu.broadcastPractice.FORCE_OFFLINE广播的接收器都会接受到该条广播。
public void onClick(View v) {
switch (v.getId()) {
case R.id.three_forceButton:
Intent intent=new Intent("com.xu.broadcastPractice.FORCE_OFFLINE");
sendBroadcast(intent);
break;
default:
break;
}
}
定义广播接收器
广播接收器都要继承BroadcastReceiver类,然后重写他的onReceive方法,在该程序中,我们接受到广播后会掏出一个对话框,setCancelable(false)设置不能点击取消,然后设置了点击ok键的处理,利用ActivityManager将所有activity finish掉,并且重新启动登陆界面
并且设置对话框类型为系统弹出
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
注意加上权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
public class ForceOffLineBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(final Context context, Intent intent) {
AlertDialog dialog= new AlertDialog.Builder(context).setTitle("worning!").setMessage("You are forced to "
+ "be offline.Please try to login again.").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityManager.getInstance().clear();
Intent intent=new Intent(context,SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}).create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
}
}
在配置文件中定义广播接收器,作为android的四大组件,都需要在AndroidMamifest.xml文件中进行配置,配置代码如下
<receiver
android:name=".ForceOffLineBroadcastReceiver"
>
<intent-filter >
<action android:name="com.xu.broadcastPractice.FORCE_OFFLINE"/>
</intent-filter>
</receiver>
name属性为自己定义的广播接收器,然后intent-filter标签下的action属性name 为需要接收的广播值,这样定以后,ForceOffLineBroadcastReceiver接收器将会接收到广播值为com.xu.broadcastPractice.FORCE_OFFLINE的广播。
运行结果

这样用户就只能点击OK,并且回到登陆界面
我们也可以动态注册广播,修改ThreeActivity的代码进行动态注册,并且将AndroidManifest.xml中注册的broadcastReceiver删掉,注意在onDestroy方法中取消注册,运行程序我们可以得到相同的效果
public class ThreeActivity extends BaseActivity implements OnClickListener{
private Button mForceButton;
private IntentFilter mIntentFilter;
private ForceOffLineBroadcastReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_three);
mIntentFilter=new IntentFilter();
mIntentFilter.addAction("com.xu.broadcastPractice.FORCE_OFFLINE");
mReceiver=new ForceOffLineBroadcastReceiver();
registerReceiver(mReceiver, mIntentFilter);
}
@Override
public void findId() {
mForceButton=(Button) findViewById(R.id.three_forceButton);
}
@Override
public void setListener() {
mForceButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.three_forceButton:
Intent intent=new Intent("com.xu.broadcastPractice.FORCE_OFFLINE");
sendBroadcast(intent);
break;
default:
break;
}
}
@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
}
|