广播
1.广播的动态注册
public class MainActivity extends AppCompatActivity {
private NetworkChangeReceive mNetworkChangeReceive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
mNetworkChangeReceive = new NetworkChangeReceive();
registerReceiver(mNetworkChangeReceive, intentFilter);
}
class NetworkChangeReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable()) {
Toast.makeText(MainActivity.this, "network 可用", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,"network 不可用",Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mNetworkChangeReceive);
}
}
清单文件权限申请
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
2.发送标准广播
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction("com.zyp.broadcastdemo.MyReceiver");
sendBroadcast(intent);
}
});
}
}
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
// throw new UnsupportedOperationException("Not yet implemented");
Toast.makeText(context, "广播来了", Toast.LENGTH_SHORT).show();
}
}
<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"
tools:context="com.zyp.broadcastdemo.MainActivity">
<Button
android:layout_width="match_parent"
android:text="点击"
android:id="@+id/bt"
android:layout_height="wrap_content"/>
</RelativeLayout>
3.接收本地广播
public class MainActivity extends AppCompatActivity {
private LocalBroadcastManager mLocalBroadcastManager;
private LocalReceive mLocalReceive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction("com.zyp.broadcastdemo.MyReceiver");
mLocalBroadcastManager.sendBroadcast(intent);
}
});
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.zyp.broadcastdemo.MyReceiver");
mLocalReceive = new LocalReceive();
mLocalBroadcastManager.registerReceiver(mLocalReceive, intentFilter);
}
class LocalReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"接收本地广播",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocalBroadcastManager.unregisterReceiver(mLocalReceive);
}
}
广播实现强制下线
public class BaseActivity extends AppCompatActivity {
private ForceOfflineReceive mOfflineReceive;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("zyp.test");
mOfflineReceive = new ForceOfflineReceive();
registerReceiver(mOfflineReceive, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
if (mOfflineReceive != null) {
unregisterReceiver(mOfflineReceive);
mOfflineReceive = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
class ForceOfflineReceive extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("warning");
builder.setMessage("强制下线");
builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCollector.finishAll();
Intent intent = new Intent(context, LoginActivity.class);
context.startActivity(intent);
}
});
builder.show();
}
}
}
public class ActivityCollector {
public static List<Activity> sActivities = new ArrayList<>();
public static void addActivity(Activity activity) {
sActivities.add(activity);
}
public static void removeActivity(Activity activity) {
sActivities.remove(activity);
}
public static void finishAll() {
for (Activity activity:sActivities) {
if (!activity.isFinishing()){
activity.finish();
}
}
sActivities.clear();
}
}
public class LoginActivity extends AppCompatActivity {
private EditText mEt_account;
private EditText mEt_pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mEt_account = (EditText) findViewById(R.id.et_account);
mEt_pwd = (EditText) findViewById(R.id.et_pwd);
Button bt_login = (Button) findViewById(R.id.bt_login);
bt_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String account = mEt_account.getText().toString().trim();
String pwd = mEt_pwd.getText().toString().trim();
if (account.equals("admin") && pwd.equals("123456")) {
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"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zyp.broadcastdemo.LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
android:textSize="15sp"/>
<EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="admin"
android:hint="请输入账号"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="15sp"/>
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:text="123456"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:textSize="15sp"/>
</LinearLayout>
<Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"/>
</LinearLayout>
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("zyp.test");
sendBroadcast(intent);
}
});
}
}
<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"
tools:context="com.zyp.broadcastdemo.MainActivity">
<Button
android:layout_width="match_parent"
android:text="点击我强制下线"
android:id="@+id/bt"
android:layout_height="wrap_content"/>
</RelativeLayout>