接收系统广播
例(系统移动网络连接状态)
class NetworkChangeReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager= (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo networkInfo= null; if (connectivityManager != null) { networkInfo = connectivityManager.getActiveNetworkInfo(); } if (networkInfo!=null&&networkInfo.isAvailable()){ //添加逻辑 }else { //添加逻辑 } } }
tips:需要添加ACCESS_NETWORK_STATE权限
静态注册开机启动:
Exported:是否允许这个广播接收器接受本程序以外的广播
Enabled:是否启用这个广播接收器
添加权限 RECEIVE_BOOT_COMPLETED
android系统启动后会发出值为android.intent.action.BOOT_COMPLETED的广播,所以在声明中<Intent-filter>下,添加相应的action
强制下线:
1.工具类ActivityCollector
2.创建BaseActivity类作为所有活动的父类
public class BaseActivity extends AppCompatActivity { private ForceOfflineReceiver receiver; @Override public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); ActivityCollector.addActivity(this); } @Override protected void onResume() { super.onResume(); IntentFilter intentFilter=new IntentFilter(); intentFilter.addAction("com.example.yrc.broadcastbestpractice.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.Please try to login again."); 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(); } }
登陆界面xml
<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" android:stretchColumns="1" tools:context="com.example.yrc.broadcastbestpractice.LoginActivity"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="Account:" android:layout_column="0" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="Input your acccount" android:id="@+id/account" android:layout_column="1" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="Passport" android:layout_column="0" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/password" android:layout_column="1" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:text="记住密码" android:id="@+id/checkBox" android:layout_column="0" android:layout_span="2" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="Login" android:id="@+id/login" android:layout_column="0" android:layout_span="2" /> </TableRow> </TableLayout>
LoginActivity:
public class LoginActivity extends AppCompatActivity { private EditText accountEdit; private EditText passwordEdit; private CheckBox checkBox; private SharedPreferences.Editor editor; private SharedPreferences pref; private Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); pref= PreferenceManager.getDefaultSharedPreferences(this); accountEdit= (EditText) findViewById(R.id.account); passwordEdit= (EditText) findViewById(R.id.password); checkBox= (CheckBox) findViewById(R.id.checkBox); login= (Button) findViewById(R.id.login); boolean isRemember=pref.getBoolean("remember_password",false); if (isRemember){ String account=pref.getString("account",""); String password=pref.getString("password",""); accountEdit.setText(account); passwordEdit.setText(password); checkBox.setChecked(true); } login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String account=accountEdit.getText().toString(); String password=passwordEdit.getText().toString(); if (account.equals("admin")&&password.equals("123456")){ editor=pref.edit(); if (checkBox.isChecked()){ editor.putBoolean("remember_password",true); editor.putString("account",account); editor.putString("password",password); }else { editor.clear(); } editor.apply(); Intent intent=new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); finish(); }else { Toast.makeText(LoginActivity.this,"account or password is invalid",Toast.LENGTH_SHORT).show(); } } }); } }
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 view) { Intent intent=new Intent("com.example.yrc.broadcastbestpractice.FORCE_OFFLINE"); sendBroadcast(intent); } }); } }
tips:在onResume和onPause中注册和取消注册,保证只有处于栈顶的活动才能接受这条强制下线广播。