android广播(内部类)使用

  1.广播定义在一个单独的文件中

源码:

  1. public class MessageReceiver extends BroadcastReceiver{  
  2.       
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.         // TODO Auto-generated method stub  
  6.         if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){  
  7.           
  8.         }  
  9.     }  
  10. }  
public class MessageReceiver extends BroadcastReceiver{
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
		
		}
	}
}

在Manifest中注册:

  1. <receiver android:name=".MessageReceiver">  
  2.             <intent-filter>  
  3.                 <action android:name="android.provider.Telephony.SMS_RECEIVED"/>  
  4.             </intent-filter>  
  5. </receiver>  
<receiver android:name=".MessageReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
</receiver>

使用代码注册:

  1. //生成广播处理    
  2. MessageReceiver  smsBroadCastReceiver = new MessageReceiver ();    
  3. //实例化过滤器并设置要过滤的广播    
  4. IntentFilter intentFilter = new IntentFilter();    
  5. intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");    
  6.     
  7. //注册广播    
  8. this.registerReceiver(smsBroadCastReceiver, intentFilter);    
//生成广播处理  
MessageReceiver  smsBroadCastReceiver = new MessageReceiver ();  
//实例化过滤器并设置要过滤的广播  
IntentFilter intentFilter = new IntentFilter();  
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");  
  
//注册广播  
this.registerReceiver(smsBroadCastReceiver, intentFilter);  

注销广播:

  1. unregisterReceiver(smsBroadCastReceiver);  
unregisterReceiver(smsBroadCastReceiver);

2.广播是内部类

  1. public class SettingForSMS extends PreferenceActivity{  
  2.       
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         addPreferencesFromResource(R.layout.sms_setting);  
  7.     }  
  8.       
  9.     public static class MessageChangeReceiver extends BroadcastReceiver {  
  10.         @Override  
  11.         public void onReceive(Context context, Intent intent) {  
  12.             // TODO Auto-generated method stub  
  13.             Log.i(tag, "onReceive");  
  14.         }  
  15.     }  
  16. }  
public class SettingForSMS extends PreferenceActivity{
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.layout.sms_setting);
	}
	
	public static class MessageChangeReceiver extends BroadcastReceiver {
	    @Override
	    public void onReceive(Context context, Intent intent) {
	        // TODO Auto-generated method stub
	        Log.i(tag, "onReceive");
	    }
	}
}

注意定义的过程中必须声明为static,不然会找不到

在Manifest中注册:

  1. <receiver android:name=".SettingForSMS$MessageChangeReceiver">  
  2.             <intent-filter>  
  3.                 <action android:name="com.ginwave.message_changed"/>  
  4.             </intent-filter>  
  5. </receiver>  
<receiver android:name=".SettingForSMS$MessageChangeReceiver">
            <intent-filter>
                <action android:name="com.ginwave.message_changed"/>
            </intent-filter>
</receiver>

注意内部类使用$符号而不是.,使用.会出现类找不到异常

在程序内注册,register是放在onCreate中,unregister是放在onDestroy中。

  1. smsBroadCastReceiver = new MessageChangeReceiver ();    
  2.         //实例化过滤器并设置要过滤的广播    
  3.         IntentFilter intentFilter = new IntentFilter();    
  4.         intentFilter.addAction("com.ginwave.message_changed");    
  5.             
  6.         //注册广播    
  7.         this.registerReceiver(smsBroadCastReceiver, intentFilter);   
smsBroadCastReceiver = new MessageChangeReceiver ();  
		//实例化过滤器并设置要过滤的广播  
		IntentFilter intentFilter = new IntentFilter();  
		intentFilter.addAction("com.ginwave.message_changed");  
		  
		//注册广播  
		this.registerReceiver(smsBroadCastReceiver, intentFilter); 
  1. unregisterReceiver(smsBroadCastReceiver);  
unregisterReceiver(smsBroadCastReceiver);

希望以后使用的时候方便点~~

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。