广播组件的实践——短信黑名单

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengbao315/article/details/51011358

转载请注明出处:http://blog.csdn.net/chengbao315/article/details/51011358

相关阅读:

Android服务组件案例:http://blog.csdn.net/chengbao315/article/details/50997218


上回书我提到了想要编写安卓四大组件案例的想法,那么说到做到,这次我就来做一个Android广播组件的案例。这次想要模仿手机360软件的短信黑名单功能,可以实现号码加入黑名单,后台运行程序,短信来到时进行拦截,并可以在软件中查看信息内容的基本功能。比如这个手机360软件的截图:


读了郭神的《第一行代码》,再加上之前几篇博客的总结,我对这个功能的实现胸有成竹,不过没有思路的童鞋也不用急,请参考《第一行代码》第5章、第8章内容。

下面来实现功能,在Eclipse中新建一个Android项目,打开activity_main.xml文件,编写界面布局。这里只简单实现需求功能,所以布局起来比较简单,只包含几个TextView控件、一个EditText控件和一个Button控件,设计代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="@string/EnterNum" />

        <EditText
            android:id="@+id/editNumber"
            android:layout_width="150dp"
            android:layout_height="wrap_content"/>

    </LinearLayout>

    <Button
        android:id="@+id/btnAddBlack"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="加入黑名单" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp" >

        <TextView
            android:id="@+id/senderText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_gravity="bottom"
            android:text="黑名单号码:" />

        <TextView
            android:id="@+id/sender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/contentText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="信息内容:" />

        <TextView
            android:id="@+id/content"
            android:layout_width="210dp"
            android:layout_height="280dp" />

    </LinearLayout>

</LinearLayout>

以上代码实现的效果如图所示:


接下来实现接收信息的功能。安卓手机在接收短信的时候,系统会发出一条值为android.provider.Telephony.SMS_RECEIVED的广播,这条广播里携带着与短信相关的所有数据。通过对这条广播的监听,在接收到广播时对信息内容进行解析,根据发送号码判断决定是否截断广播,即可实现短信黑名单的功能。

打开MainActivity.java 文件,在onCreate()方法中添加动态注册监听短信广播的代码,同时获取信息号码、信息内容TextView和Button的实例,添加代码如下:

public class MainActivity extends Activity implements OnClickListener {
    private TextView sender;
    private TextView content;
    private IntentFilter receiveFilter;
    private MessageReceiver messageReceiver;
    private static String BlackNum = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sender = (TextView) findViewById(R.id.sender);
        content = (TextView) findViewById(R.id.content);
        Button btnAdd = (Button) findViewById(R.id.btnAddBlack);
        btnAdd.setOnClickListener(this);

        receiveFilter = new IntentFilter();
        receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        receiveFilter.setPriority(100);
        messageReceiver = new MessageReceiver();
        registerReceiver(messageReceiver, receiveFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(messageReceiver);
    }
为了实现软件先于手机自带信息软件收到信息,通过调用 receiveFilter.setPriority(100);方法设置软件的优先级较高。接下来在MainActivity中新建MessageReceiver内部类,继承BroadcastReceiver,实现接收广播后的处理逻辑,代码如下所示:

private class MessageReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Log.d("MessageReceiver", "Receive");
			Bundle bundle = intent.getExtras();
			Object[] pdus = (Object[]) bundle.get("pdus");
			SmsMessage[] message = new SmsMessage[pdus.length];
			for (int i = 0; i < message.length; i++) {
				message[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
			}
			// 获取手机号码
			String Number = message[0].getOriginatingAddress();
			String fullMessage = "";
			for (SmsMessage m : message) {
				fullMessage += m.getMessageBody();// 获取短信内容
			}
			Log.d("MessageReceiver[Number]", Number);
			Log.d("MessageReceiver[Message]", fullMessage);
			if (BlackNum.equals(Number)) {
				NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
				Notification notification = new Notification(
						R.drawable.ic_launcher, "拦截黑名单信息",
						System.currentTimeMillis());
				Intent notificationIntent = new Intent(context,
						MainActivity.class);
				PendingIntent pendingIntent = PendingIntent.getActivity(
						context, 0, notificationIntent, 0);
				notification.setLatestEventInfo(context, "新信息", "拦截黑名单信息",
						pendingIntent);
				notification.defaults = Notification.DEFAULT_ALL;
				manager.notify(1, notification);

				sender.setText(Number);
				content.setText(fullMessage);
				abortBroadcast();
			}
		}
	}
在接收到广播后,系统调用onReceive()方法,通过Bundle方式传递信息数据,通过对Bundle的解析,将信息内容存储到一个SmsMessage数组中,进而完成信息的接收。接下来对比来信号码与黑名单号码是否相等,如果相等,给出一个通知(这里不是需求的要求内容,以后再详细讲),在界面显示信息号码和信息内容,同时截断广播向手机的信息软件传播。
最后别忘了在AndroidManifest.xml中的代码中给程序声明一个接收短信的权限"android.permission.RECEIVE_SMS"(另一个权限是手机通知的震动权限),添加代码如下:

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
以上代码实现了软件接收信息和拦截信息的功能,但是设置黑名单的功能还没有实现,继续编辑MainActivity.java文件,添加Button的监听代码,在代码中获取EditText的内容,保存为黑名单号码,添加代码如下:
public class MainActivity extends Activity implements OnClickListener {
	......

	@Override
	public void onClick(View view) {
		EditText text = (EditText) findViewById(R.id.editNumber);
		if (!text.getText().toString().isEmpty()) {
			BlackNum = text.getText().toString();
			Toast.makeText(this, "设置黑名单号码为:" + BlackNum, Toast.LENGTH_LONG)
					.show();
			Log.d("MainActivity[Number]", BlackNum);
		}
	}
}
还有一个隐含的功能,就是后台运行,其实这段代码完全可以不写,通过按Home键代替,但是提到了还是实现以下吧。其实原理很简单,就是捕捉Back点击事件,然后设置为后台运行,在MainActivity中继续添加代码如下:
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			moveTaskToBack(true);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
激动人心的时刻到了,最后的最后就是来进行测试了。在郭神的《第一行代码》中,提供了DDMS模拟器的方法(可以参考8.2.1章节),但是我的电脑根本没装模拟器,又不想花信息费,那怎么办?机智的我决定给10086发信息吧,结果查了一下午的话费,估计10086也快疯了。测试结果如下:

  


下载源码,点击这里!

猜你喜欢

转载自blog.csdn.net/chengbao315/article/details/51011358