Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 6.0監聽短信(有Git真好,簡單方便+輕松愉快)

Android 6.0監聽短信(有Git真好,簡單方便+輕松愉快)

編輯:關於Android編程

最近在最後的任職期還被搾了一頓忙的飛起,不過今天是最後一天了,幫著處理個BUG就功德圓滿了。然後最近在做一個手機間通信+XX監聽的Demo,其中一部分是關於短信監聽的,然後正好6.0授權的一些因素,那就把這部分的實現單獨拆出來做這麼個Demo,順道把這幾天覺得發現的不錯的第三方庫貢獻給大家

先來看一下運行效果

這裡寫圖片描述

先是配置把要過濾的短信字段輸入

輸入框控件

 compile 'com.github.florent37:materialtextfield:1.0.5'

然後用一自定義RV來顯示/處理輸入的內容

    compile 'com.nikhilpanju.recyclerviewenhanced:recyclerviewenhanced:1.0.0'

然後用戶輸完就跳轉到監聽頁面,當有短信了,顯示如下

這裡寫圖片描述

這裡寫圖片描述

再來看下 項目結構<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPjxpbWcgYWx0PQ=="這裡寫圖片描述" src="/uploadfile/Collfiles/20160702/20160702091838643.png" title="\" />

用到的庫的清單


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.apkfuns.logutils:library:1.4.2'
    compile 'com.jakewharton:butterknife:8.1.0'
    compile 'com.github.navasmdc:MaterialDesign:1.5@aar'
    apt 'com.jakewharton:butterknife-compiler:8.1.0'
    compile 'com.nikhilpanju.recyclerviewenhanced:recyclerviewenhanced:1.0.0'
    compile 'com.github.florent37:materialtextfield:1.0.5'
    compile 'com.lovedise:permissiongen:0.0.6'
}

OK,來一步步讀代碼,先從配置開始

public class SettingActivity extends BaseActivity {
    @BindView(R.id.input)
    EditText input;
    @BindView(R.id.add)
    ButtonRectangle add;
    @BindView(R.id.jump)
    ButtonRectangle jump;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private MainAdapter mAdapter;

    private RecyclerTouchListener onTouchListener;
    //數據集合
    private List list;

    @Override
    int getLayout() {
        return R.layout.activity_setting;
    }

    @Override
    void init() {
        ButterKnife.bind(this);
        list = new ArrayList<>();
        mAdapter = new MainAdapter(this, list);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        onTouchListener = new RecyclerTouchListener(this, recyclerView);
    }

    @Override
    void logic() {
        //注冊滑動刪除行為
        onTouchListener.setSwipeOptionViews(R.id.edit)
                .setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
                    @Override
                    public void onSwipeOptionClicked(int viewID, int position) {
                        if (viewID == R.id.edit) {
                            list.remove(position);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                });
        recyclerView.addOnItemTouchListener(onTouchListener);
    }

    @Override
    void onResumeInit() {
        //刷新適配器
        if (list.size() > 0) {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    void onResumeLogic() {

    }

    @Override
    protected void onPause() {
        super.onPause();
        //釋放無用資源
        recyclerView.removeOnItemTouchListener(onTouchListener);
    }

    @OnClick({R.id.add, R.id.jump})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add:
                String value = input.getText().toString().trim();
                if (!value.isEmpty()) {
                    list.add(value);
                    mAdapter.notifyDataSetChanged();
                    input.setText("");
                }
                break;
            case R.id.jump:

                if (list.size() > 0) {
                    Intent intent = new Intent(SettingActivity.this, MainActivity.class);
                    intent.putStringArrayListExtra("dataList", (ArrayList) list);
                    startActivity(intent);
                } else {
                    Toast.makeText(getApplicationContext(), "請添加過濾內容", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}

具體重要步驟已經在注解裡寫著了,主要大家可以看下這個RV庫,支持部分點擊,全局點擊和自定義滑動刪除在 Android">https://github.com/ddwhan0123/Useful-Open-Source-Android的 RV部分有收納

再來看下主類

public class MainActivity extends BaseActivity {
    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
    private SMSBroadcastReceiver mSMSBroadcastReceiver;
    private List intentList;
    @BindView(R.id.text)
    TextView text;

    @Override
    int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    void init() {
        ButterKnife.bind(this);
        //接受傳遞來的過濾集合
        intentList = getIntent().getStringArrayListExtra("dataList");
    }

    @Override
    void logic() {
        //授權
        PermissionGen.with(MainActivity.this)
                .addRequestCode(100)
                .permissions(
                        Manifest.permission.RECEIVE_SMS,
                        Manifest.permission.READ_SMS)
                .request();

        //生成廣播處理
        mSMSBroadcastReceiver = new SMSBroadcastReceiver();
        mSMSBroadcastReceiver.setOnReceivedMessageListener(new SMSBroadcastReceiver.MessageListener() {
            @Override
            public void onReceived(PhoneMessage phoneMessage) {
                for (int k = 0; k < intentList.size(); k++) {
                    if (phoneMessage.getMsgContent().contains(intentList.get(k))) {
                        String msg = phoneMessage.getPhoneNumber() + "*" + phoneMessage.getMsgTime() + "*" + phoneMessage.getMsgContent();
                        LogUtils.d("--->發送的對象是 " + msg);
                        text.setText(msg);
                    }
                }

            }
        });
    }

    @Override
    void onResumeInit() {


    }

    @Override
    void onResumeLogic() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注銷廣播
        if (mSMSBroadcastReceiver != null) {
            this.unregisterReceiver(mSMSBroadcastReceiver);
            mSMSBroadcastReceiver = null;
        }
    }


    @PermissionSuccess(requestCode = 100)
    public void doRegisterReceiver() {
        //實例化過濾器並設置要過濾的廣播
        IntentFilter intentFilter = new IntentFilter(ACTION);
        intentFilter.setPriority(1000);
        //注冊廣播
        this.registerReceiver(mSMSBroadcastReceiver, intentFilter);
    }

    @PermissionFail(requestCode = 100)
    public void doFailRegisterReceiver() {
        Toast.makeText(this, "Contact permission is not granted", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }
}

這個類在一開始申請讀取短信的授權,然後根據授權成功與否的回調來開啟廣播或者彈出失敗的Toast(實際生產時記得經常看看廣播在不在,因為授權行為只有在裝的第一次提示)

這邊用的是動態注冊廣播,畢竟常年監視用戶的行為我是拒絕的,生活所迫沒辦法。

public class SMSBroadcastReceiver extends BroadcastReceiver {


    private static MessageListener mMessageListener;
    public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";

    public SMSBroadcastReceiver() {
        super();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        LogUtils.d("----> SMSBroadcastReceiver onReceive");
        if (intent.getAction().equals(SMS_RECEIVED_ACTION)) {
            Object[] pdus = (Object[]) intent.getExtras().get("pdus");
            for (Object pdu : pdus) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
                String sender = smsMessage.getDisplayOriginatingAddress();
                //短信內容
                String content = smsMessage.getDisplayMessageBody();
                long date = smsMessage.getTimestampMillis();
                Date tiemDate = new Date(date);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = simpleDateFormat.format(tiemDate);
                PhoneMessage phoneMessage = new PhoneMessage();
                phoneMessage.setPhoneNumber(sender);
                phoneMessage.setMsgTime(time);
                phoneMessage.setMsgContent(content);

                mMessageListener.onReceived(phoneMessage);
                abortBroadcast();
            }
        }

    }

    //回調接口
    public interface MessageListener {
        void onReceived(PhoneMessage phoneMessage);
    }

    public void setOnReceivedMessageListener(MessageListener messageListener) {
        this.mMessageListener = messageListener;
    }


}

廣播和主Activity是通過接口互相通信的,並沒用Message什麼的,覺得接口比較簡單實現,封裝了一個“消息對象”,傳遞著手機號/時間/具體內容

過濾的行為交給Activity做,不希望廣播內部過於復雜(可以考慮改成MVP做)

源碼地址:https://github.com/ddwhan0123/BlogSample/tree/master/GetSMSDemo

下載鏈接:https://github.com/ddwhan0123/BlogSample/blob/master/GetSMSDemo/GetSMSDemo.zip?raw=true

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved