新建广播接收器:
class NetworkChangeReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context,"network changes",Toast.LENGTH_SHORT).show(); } }注册广播接收器:
private IntentFilter mIntentFilter; private NetworkChangeReceiver mNetworkChangeReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mIntentFilter = new IntentFilter(); mIntentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); mNetworkChangeReceiver = new NetworkChangeReceiver(); registerReceiver(mNetworkChangeReceiver,mIntentFilter); }完成。。
静态注册:
1.新建广播接收器:
public class BootCompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context,"Boot Complete",Toast.LENGTH_SHORT).show(); } }2.在manifest.xml中静态注册需要接收的广播
<receiver android:name=".BootCompleteReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>完成。。
发送广播:
Intent intent = new Intent("com.lewanjiang.broadcasttest.MY_BORADCAST"); sendBroadcast(intent);发送有序广播:
sendOrderedBroadcast(intent);
发送本地广播:
LocalBroadcastManager locBroMan = LocalBroadcastManager.getInstance(this);
Intent int = new Intent("com.lewanjiang.broadcasttest.LOCAL_BROADCAST");
locBroMan.sendBroadcast(int);
接收注册同普通广播一样,只是注销时:locBroMan.unregisterReceiver(localReceiver);
强制下线功能实现:
1.新建ActivityCollector类和BaseActivity类:
public class ActivityCollector { public static List<Activity> activities = new ArrayList<>(); public static void addActivity(Activity activity) { activities.add(activity); } public static void removeActivity(Activity activity) { activities.remove(activity); } public static void finishAll() { for (Activity activity:activities) { if (!activity.isFinishing()) { activity.finish(); } } } }
public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityCollector.addActivity(this); } @Override protected void onDestroy() { super.onDestroy(); ActivityCollector.removeActivity(this); }2.新建一个空的Activity:LoginActivity,修改相应的activity_login.xml和LoginAcitivity:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.lewanjiang.broadcastbestpractice1.LoginActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:text="Account:" android:layout_width="90dp" android:layout_height="wrap_content"/> <EditText android:id="@+id/account" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:text="Password:" android:layout_width="90dp" android:layout_height="wrap_content"/> <EditText android:id="@+id/password" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"/> </LinearLayout> <Button android:id="@+id/login" android:text="login" android:layout_width="match_parent" android:layout_height="60dp"/> </LinearLayout>
public class LoginActivity extends BaseActivity { private EditText accountEdit; private EditText passwordEdit; private Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); accountEdit = (EditText) findViewById(R.id.account); passwordEdit = (EditText) findViewById(R.id.password); login = (Button) findViewById(R.id.login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String account = accountEdit.getText().toString(); String password = passwordEdit.getText().toString(); if (account.equals("admin") && password.equals("123456")) { Intent intent = new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); finish(); } else { Toast.makeText(LoginActivity.this,"account or password wrong",Toast.LENGTH_SHORT).show(); } } }); } }3.修改activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.lewanjiang.broadcastbestpractice1.MainActivity"> <Button android:id="@+id/force_offline" android:text="send force offline broadcast" android:layout_width="match_parent" android:layout_height="wrap_content"/> </android.support.constraint.ConstraintLayout>4.修改MainActivity:
public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button forceOffline = (Button) findViewById(R.id.force_offline); forceOffline.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.lewanjiang.broadcastbestpractice1.FORCE_OFFLINE"); sendBroadcast(intent); } }); } }5.注册广播接收器,修改BaseActivity如下:
public class BaseActivity extends AppCompatActivity { private ForceOfflineReceiver receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityCollector.addActivity(this); } @Override protected void onResume(){ super.onResume(); IntentFilter intentFilter = new IntentFilter("com.lewanjiang.broadcastbestpractice1.FORCE_OFFLINE"); receiver = new ForceOfflineReceiver(); 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 ForceOfflineReceiver extends BroadcastReceiver{ @Override public void onReceive(final Context context, Intent intent) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Warning"); builder.setMessage("You are forced to be offline"); builder.setCancelable(false); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ActivityCollector.finishAll(); Intent intent1 = new Intent(context,LoginActivity.class); context.startActivity(intent1); } }); builder.show(); } } }