android-BroadcastReceiver 通过终止广播 阻止用户收到短信

1,系统收到短信,发出的广播属于有序广播。

如果想阻止用户收到短信,可自定义Receiver,设置高优先级,率先获得接收短信的广播,并终止广播。

2,接收短信的广播名android.provider.Telephony.SMS_RECEIVED

3,注意:程序一旦在某个模拟器运行,将一直阻止短信,只有注释掉abortBroadcast(),重新运行,方可正常。


manifest




    

    
        
            
                

                
            
        
        
            
                
            
        
    
    


主activity

package com.song;

import android.app.Activity;
import android.os.Bundle;

public class C49_BroadcastSmsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

receiver

package com.song;

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

public class MySmsResevicer extends BroadcastReceiver{

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




你可能感兴趣的:(android,其他)