안 드 로 이 드 노트 는 어떻게 문자 의 실시 간 내용 을 얻 습 니까?
                                            
 6061 단어  안 드 로 이 드 노트
                    
먼저 문자 메 시 지 를 어떻게 얻 는 지 살 펴 보 자. 핸드폰 의 주소록 정보, 문자 메 시 지 는 모두 ContentProvider 에 저장 되 어 있다.저희 가 문 자 를 감청 하려 면 콘 텐 츠 관찰자 인 ContentObserver 를 사용 해 야 합 니 다. ContentObserver 에 대해 잘 모 르 시 면 여 기 를 클릭 해서 우회전 하 세 요.http://blog.csdn.net/qinjuning/article/details/7047607 우 리 는 콘 텐 츠 Observer 를 계승 하여 문자 에 대한 감청 을 실현 한다.문자 읽 기 권한 추가
 
  package com.example.demoobsever;
import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class MainActivity extends Activity {
    private TextView vSms;//    TextView
    private SMSContent smsObsever;//     
    private Handler handler =new Handler(){
        public void handleMessage(android.os.Message msg) {
            Bundle bundle=msg.getData();
            String body=bundle.getString("body");
            vSms.setText(body);
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vSms=(TextView) this.findViewById(R.id.tx_sms);//      
        smsObsever=new SMSContent(handler);//        
    //       
            getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, smsObsever);
    }
    /**
     * @description      
     * @author Administrator
     *
     */
    class SMSContent extends ContentObserver {
        private Handler mHandler;
        public SMSContent(Handler handler) {
            super(handler);
            mHandler=handler;
        }
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Cursor cursor = null;
            String body=null;
            try {
                cursor = getContentResolver().query(
                        Uri.parse("content://sms/inbox"), null, null, null,
                        "date desc");
                if(cursor!=null){
                    if(cursor.moveToNext()){//              
                        //         
                        body=cursor.getString(cursor.getColumnIndex("body"));
                        Message msg=Message.obtain();
                        Bundle bundle=new Bundle();
                        bundle.putString("body", body);
                        msg.setData(bundle);
                        mHandler.sendMessage(msg);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(cursor!=null){
                    cursor.close();
                }
            }
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //    
        getContentResolver().unregisterContentObserver(smsObsever);
    }
}
문자 내용 을 가 져 오 는 것 입 니 다. 인증 코드 를 가 져 오 려 면 Pattern 으로 일치 하면 됩 니 다.여 기 는 더 이상 소개 하지 않 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
학습 노트: Android 의 MVC 모드 와 MVP 모드업무 논리, 데이터, 인터페이스 로 분 리 된 방법 으로 코드 를 구성 하고 업무 논 리 를 한 위 젯 에 모 으 며 개성 화 된 맞 춤 형 인터페이스 와 사용자 의 상호작용 을 개선 하 는 동시에 업무 논 리 를 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.