温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android 广播接受者

发布时间:2020-05-25 21:46:53 来源:网络 阅读:495 作者:老婆的宝宝 栏目:移动开发

广播也是通过intent来传递的。

    广播分为有序广播和标准广播。

        标准广播是发送广播后,所有的广播接受者都可以去接收。

        有序广播是发送广播后,由高优先级的先接收广播,处理后再往后广播,同时高优先级的接受者可以中断广播。


        广播注册可以分为动态注册和静态注册。下面就先将静态注册。静态注册是新建广播接收者时,是通过new----->other------->Brodcast Receiver来实现的。AS会自动帮我们在Manifext.xml里注册好,我们只需要添加intent-filter及在里面加入action即可。

    

    下面的例子是静态注入,发送一条标准广播,然后接收。

    

    1、MainActivity

    

package com.yuanlp.sendbroadcast; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }     public void click(View view){         Intent intent=new Intent("com.yuanlp.sendBroadcast.MY_BROADCAST"); //设置intent的action         sendBroadcast(intent);  //发送广播     } }

    2、 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.yuanlp.sendbroadcast.MainActivity">     <Button         android:id="@+id/button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:onClick="click"         android:text="发送广播"         tools:layout_editor_absoluteX="137dp"         tools:layout_editor_absoluteY="136dp"/> </android.support.constraint.ConstraintLayout>

     3、MyReceiver

    

package com.yuanlp.sendbroadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver {     @Override     public void onReceive(Context context, Intent intent) {         Toast.makeText(context,"接收到广播",Toast.LENGTH_SHORT).show();     } }

        4、Manifext.xml

    

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"           package="com.yuanlp.sendbroadcast">     <application         android:allowBackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:roundIcon="@mipmap/ic_launcher_round"         android:supportsRtl="true"         android:theme="@style/AppTheme">         <activity android:name=".MainActivity">             <intent-filter>                 <action android:name="android.intent.action.MAIN"/>                 <category android:name="android.intent.category.LAUNCHER"/>             </intent-filter>         </activity>         <receiver             android:name=".MyReceiver"             android:enabled="true"             android:exported="true">             <intent-filter>                 <action android:name="com.yuanlp.sendBroadcast.MY_BROADCAST"></action>             </intent-filter>         </receiver>     </application> </manifest>

    运行程序后,在点击按钮后,自定义的广播 接受者会受到广播,并弹出toast

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI