On the usage of BroadcastReceiver

Transfer: http://www.cnblogs.com/shang53880/archive/2011/02/14/1954420.html

Record it, for later use.

Under recently read something about broadcasting deepen understanding, but also corrected his previous incorrect views.

There are 2 registered on the broadcast, one is registered in the code, also known as dynamic registration, the other is registered in AndroidManifest inside, also called static registration.

No matter what the inside registration, we need to send a broadcast sendBroadcast (). A broadcast transmission is Intent, intent which typically action.

If the code which is registered in the general register in which onResume () method.

@Override
    protected void onResume() {
        // TODO Auto-generated method stub
         
        myReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        //向过滤器中添加action
         filter.addAction("com.android.shang");
        //注册广播
         registerReceiver(myReceiver, filter);
        
        Intent intent = new Intent();
        intent.setAction("com.android.xiang");
        //发送广播
         sendBroadcast(intent);
        super.onResume();
    }

In onPause () method which should cancel the registration.

 @Override
2     protected void onPause() {
3         // TODO Auto-generated method stub
4         unregisterReceiver(myReceiver);
5         super.onPause();
6     }

Of course, you can also register in the xml file:

<receiver
         android:name=".MyBrocastReceiverTest">
         <intent-filter>
             <action    android:name="com.android.xiang"></action>
         </intent-filter>
     </receiver>

If the broadcast of the action sent already registered, you will receive onReceive () method to send over the radio, and then make some deal.

  You can write an inner class:

class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            //从这里的log中可以看到是否接受到了广播
            System.out.println("intent = " + intent);
        }
        
    }

Of course, you can write a separate category:

package com.android.TestBrocastReceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBrocastReceiverTest extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("intent = " + intent);
    }

}

If the general registration code inside, put the radio in the code written inside, registered in the xml file, wrote a separate category.

  Registration code inside:

package com.android.TestBrocastReceiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class TestBrocastReceiver extends Activity {
    
    MyReceiver myReceiver;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        unregisterReceiver(myReceiver);
        super.onPause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        
        myReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        //向过滤器中添加action
        filter.addAction("com.android.shang");
        //注册广播
        registerReceiver(myReceiver, filter);
        
        Intent intent = new Intent();
        intent.setAction("com.android.xiang");
        //发送广播
        sendBroadcast(intent);
        super.onResume();
    }
    
    class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            //从这里的log中可以看到是否接受到了广播
            System.out.println("intent = " + intent);
        }
    }
}

Registered in the xml file:

Registered in the xml file, written on a broadcasting class, then send broadcast in activity on it.

Sometimes the system emits some broadcasters, such as changes over time and so on, time to go second will send a broadcast, then we can listen to the radio, and then do the processing in the receiver can be, without the need to send a broadcast .

Guess you like

Origin blog.csdn.net/Smile_Qian/article/details/81704385