代码注册广播接收器

来源:互联网 发布:为什么还原网络设置 编辑:程序博客网 时间:2024/04/30 14:41

  注册广播接收器的方式:



代码:

在AndroidMainfest.xml中增加修改后的代码

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.haige.broatcast">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        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=".MyReceiver1"            android:enabled="true"            android:exported="true">            <intent-filter><!--增加的代码-->                <action android:name="com.codingke.action.MY_BROLOCAST" />            </intent-filter>   <!--增加的代码-->        </receiver>        <receiver            android:name=".MyReceiver2"            android:enabled="true"            android:exported="true"></receiver>    </application></manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:weightSum="1">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="发送一个普通的广播"        android:id="@+id/button_normal"        android:onClick="sendNormalClick"        android:layout_alignParentTop="true"        android:layout_alignParentEnd="true" /></RelativeLayout>

MainActivity.java

package com.example.haige.broatcast;import android.app.Activity;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {    private MyReceiver2 receiver2=new MyReceiver2();    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    /*     在该方法中进行广播注册     */    @Override    protected void onResume() {        super.onResume();        IntentFilter filter=new IntentFilter();        filter.addAction("com.codingke.action.MY_BROLOCAST");        registerReceiver(receiver2,filter);    }    /*    在该方法中进行解除注册     */    @Override    protected void onPause()    {        super.onPause();        unregisterReceiver(receiver2);    }    //发送一个普通的广播    public void sendNormalClick(View v)    {        Intent intent=new Intent("com.codingke.action.MY_BROLOCAST");//在AndroidMainfest中修改的代码        intent.putExtra("info","我们很有缘,终于对接了");      this.sendBroadcast(intent);    }}

MyReceiver1.java

package com.example.haige.broatcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyReceiver1 extends BroadcastReceiver {    public MyReceiver1() {    }    /*    自定义的广播接收器     */    @Override    public void onReceive(Context context, Intent intent) {       String info=intent.getStringExtra("info");        Toast.makeText(context,info,Toast.LENGTH_SHORT).show();    }}

MyReceiver2.java

package com.example.haige.broatcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyReceiver2 extends BroadcastReceiver {    public MyReceiver2() {    }    @Override    public void onReceive(Context context, Intent intent) {        Toast.makeText(context,"动态注册的广播接收器",Toast.LENGTH_SHORT).show();    }}


 总结:运行截图简陋不发了,当点击Button时会分别出现两条悬浮广播信息,一秒后又消失。


0 0
原创粉丝点击