发布时间:2025-12-10 11:40:43 浏览次数:11
1.案例:接收开机的广播
重启手机。
(1)布局文件
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送标准广播" android:textSize="30dp" android:onClick="sendBroadcast" />(2)Java代码
//点击按钮发送标准广播 public void sendBroadcast(View view) { //设置发送的数据 Intent intent =new Intent(); intent.setAction("com.lxz.app8"); intent.putExtra("code","我是张三!"); //动态注册 IntentFilter filter=new IntentFilter(); filter.addAction("com.lxz.app8"); BroadcastReceiver1 receiver=new BroadcastReceiver1(); registerReceiver(receiver,filter); //发送广播 sendBroadcast(intent); }(3)自定义广播接收器
//接收标准广播public class BroadcastReceiver1 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "接收信息:"+intent.getStringExtra("code"), Toast.LENGTH_SHORT).show(); }}(4)效果图
(1)manifest中定义和设置权限
<!--定义权限--> <permission android:name="com.lxz.app.permission"/> <!--设置权限--> <uses-permission android:name="com.lxz.app.permission"/>(2)布局文件
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送标准广播(带权限)" android:textSize="30dp" android:onClick="sendBroadcast2" />(3)发送广播的Java代码
//点击按钮发送标准广播(带权限) public void sendBroadcast2(View view) { //设置发送的数据 Intent intent =new Intent(); intent.setAction("com.lxz.app8"); intent.putExtra("code","我是张三!有权限!"); //动态注册 IntentFilter filter=new IntentFilter(); filter.addAction("com.lxz.app8"); BroadcastReceiver1 receiver=new BroadcastReceiver1(); registerReceiver(receiver,filter); //设置权限 String str="com.lxz.app.permission"; //发送广播 sendBroadcast(intent,str); }(4)广播接收器
//接收标准广播public class BroadcastReceiver1 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "接收信息:"+intent.getStringExtra("code"), Toast.LENGTH_SHORT).show(); }}(5)效果图
(1)布局文件
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送有序广播" android:textSize="30dp" android:onClick="sendOrderBroadcast1" />(2)发送有序广播的方法
//点击按钮发送有序广播 public void sendOrderBroadcast1(View view) { //设置发送的数据 Intent intent =new Intent(); intent.setAction("com.lxz.app8"); intent.putExtra("code","请依次报号!"); //发送广播-参数2代表的是自定义的权限 sendOrderedBroadcast(intent,null); }(3)注册代码
<!--注册广播--><receiver android:name=".BroadcastOrderReceiver"android:exported="true"><intent-filter android:priority="0"><action android:name="com.lxz.app8"/></intent-filter></receiver><receiver android:name=".BroadcastOrderReceiver1"android:exported="true"><intent-filter android:priority="1"><action android:name="com.lxz.app8"/></intent-filter></receiver><receiver android:name=".BroadcastOrderReceiver2"android:exported="true"><intent-filter android:priority="2"><action android:name="com.lxz.app8"/></intent-filter></receiver><receiver android:name=".BroadcastOrderReceiver3"android:exported="true"><intent-filter android:priority="3"><action android:name="com.lxz.app8"/></intent-filter></receiver>(4)广播接收者
(5)效果图
1.参考代码
(1)布局文件代码
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送本地广播" android:textSize="30dp" android:onClick="localBroadcast" />(2)发送本地广播的Java代码
//发送本地广播public void localBroadcast(View view) {//设置数据 Intent intent=new Intent(); intent.setAction("com.lxz.localapp"); intent.putExtra("code","我是本地广播");//动态注册 LocalBroadReceiver receiver=new LocalBroadReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction("com.lxz.localapp"); LocalBroadcastManager manager=LocalBroadcastManager.getInstance(getApplicationContext()); manager.registerReceiver(receiver,filter);//发送广播 manager.sendBroadcast(intent);}(3)本地广播接收者的代码
//接收本地广播public class LocalBroadReceiver extends BroadcastReceiver {@Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "接收信息:"+intent.getStringExtra("code"), Toast.LENGTH_SHORT).show();}}(4)效果图
待实现
1.案例要求
2.参考代码
(1)项目目录结构
(2)ActivityCtroller代码。
(3)BaseActivity,是登录成功界面的基类,可以注册BroadcastReceiver。
(4)登录界面的Activity。
//仿qq下线public class LoginActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); } //方法:登录 public void login(View view) { EditText account=findViewById(R.id.account); EditText password=findViewById(R.id.password); System.out.println(account+","+password); if (account.getText().toString().equals("root")&&password.getText().toString().equals("123456")){ Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show(); Intent intent=new Intent(); intent.setClass(getApplicationContext(),LoginSuccessActivity.class); startActivity(intent); finish(); } else{ Toast.makeText(this, "账号或密码错误!", Toast.LENGTH_SHORT).show(); } }}(5)登录界面activity对应的布局文件。
<?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:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".LoginActivity"><TableLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:stretchColumns="1"><TableRow><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="text"android:text="账号:"android:textSize="30dp" /><EditTextandroid:id="@+id/account"android:text="root"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="账号(root)"android:inputType="text"android:maxLines="1"android:textSize="30dp" /></TableRow><TableRow><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="text"android:text="密码:"android:textSize="30dp" /><EditTextandroid:id="@+id/password"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="密码(123456)"android:inputType="text"android:maxLines="1"android:text="123456"android:textSize="30dp" /></TableRow></TableLayout><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="login"android:text="登录"android:textSize="30dp" /></LinearLayout>(6)登录成功Activity,LoginSuccessActivity的代码。
(7)activity_login_success布局文件代码。
(8)LoginOutReceiver广播接收器代码。
(9)效果图