智能管家项目总结(2)

来源:互联网 发布:旋转轮胎车辆数据修改 编辑:程序博客网 时间:2024/06/06 14:16

4.监听手机收到短信

    先是广播接收器

class InnerSmsReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        L.d("sms");        //获取短信内容返回的是一个Object数组        Object[] objs = (Object[]) intent.getExtras().get("pdus");        //遍历数组得到短信内容        for (Object obj : objs) {// 超过140字节,会分多条短信发送            //把数组元素转换成短信对象            SmsMessage sms = SmsMessage.createFromPdu((byte[]) obj);            originatingAddress = sms.getOriginatingAddress();            messageBody = sms.getMessageBody();            L.i("短信号码:" + originatingAddress + ";短信内容:"                    + messageBody);            //弹框显示            showSmsWindow(originatingAddress, messageBody);        }    }}

      下面注册广播

// 拦截短信, 同等条件下,动态注册更优先获取广播mReceiver = new InnerSmsReceiver();IntentFilter filter = new IntentFilter();filter.addAction("android.provider.Telephony.SMS_RECEIVED");filter.setPriority(Integer.MAX_VALUE);registerReceiver(mReceiver, filter);自定义广播里有一个监听到短信后弹出一个窗口,下面代码:private void showSmsWindow(String title, String content) {    //窗口管理器    wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);    //布局参数    layoutParams = new WindowManager.LayoutParams();    layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;    layoutParams.flags =            //WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | 不能触摸            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;    //WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 沒有焦点    //格式    layoutParams.format = PixelFormat.TRANSLUCENT;    //类型    layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;    mView = (SessionLinearLayout) View.inflate(getApplicationContext(), R.layout.window_item, null);    tv_title = (TextView) mView.findViewById(R.id.tv_title);    tv_title.setText("发件人:" + originatingAddress);    tv_content = (TextView) mView.findViewById(R.id.tv_content);    tv_content.setText("短信内容:" + messageBody);    btnSend = (Button) mView.findViewById(R.id.btnSend);    btnSend.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View view) {            if (mView.getParent() != null) {                wm.removeView(mView);            }            //send sms            Uri uri = Uri.parse("smsto:" + originatingAddress);            Intent i = new Intent(Intent.ACTION_SENDTO, uri);            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            i.putExtra("sms_baby", "");            startActivity(i);        }    });    wm.addView(mView, layoutParams);    //监听返回键    mView.setDispatchKeyEventListener(mDispatchKeyEventListener);}

上面代码里面还有一个按键的监听,Android里面的HomeBackMenu三个键同样使用广播进行管理。

HomeWatcherReceiver mHomeKeyReceiver;//注册Home监听广播mHomeKeyReceiver = new HomeWatcherReceiver();final IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);registerReceiver(mHomeKeyReceiver, homeFilter);//监听返回键mView.setDispatchKeyEventListener(mDispatchKeyEventListener);/** * 返回鍵监听 */private SessionLinearLayout.DispatchKeyEventListener mDispatchKeyEventListener = new SessionLinearLayout.DispatchKeyEventListener() {    @Override    public boolean dispatchKeyEvent(KeyEvent event) {        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {            if (mView.getParent() != null) {                wm.removeView(mView);            }            return true;        }        return false;    }};还有一个Home键/** * 监听Home键 */class HomeWatcherReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        String action = intent.getAction();        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {            String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);            if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {                if (mView.getParent() != null) {                    wm.removeView(mView);                }            }        }    }}

后面的代码看了下,的确是基础课程,都比较简单,没什么复杂的东西,好多都是用了很多次的东西,下一篇再把零碎知识整理一下,也就完工了。哈哈  ^_^

软件github下载地址:https://github.com/SunnyGZY/SmartButler

0 0