[android-003]android 앱으로 핸드폰 문자 받기

2626 단어
1. 안드로이드 매니페스트에서.xml 설정 권한

    

2. 문자를 읽을 함수에 권한을 설정하고 문자를 읽는다
//      ,         
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_SMS)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_SMS}, 1);
        }

        //        
//        Uri uri = Uri.parse(SMS_URI_INBOX);
        Uri uri = Uri.parse(SMS_URI_ALL);

        SmsContent sc = new SmsContent(this, uri);
        //      
        infos = sc.getSmsInfo();

....

//activtiy  MainActivity    ,    context,  contect  contentResolver
        ContentResolver cr = activity.getApplicationContext().getContentResolver();

        //      /  
        String[] projection = new String[]{"_id", "address", "person",
                "body", "date", "type", "sim_id"};

        Cursor cusor = cr.query(uri, projection, null, null, "date desc");
        //       ,             ,    ,  Null
        int nameColumn = cusor.getColumnIndex("person");
        //    
        int smsbodyColumn = cusor.getColumnIndex("body");
        //  ,long , 1256539465022,             
        int dateColumn = cusor.getColumnIndex("date");
        //type:    1     ,2    
        int typeColumn = cusor.getColumnIndex("type");
        int simIdColumn = cusor.getColumnIndex("sim_id");
        //       
        int addressColumn = cusor.getColumnIndex("address");

        if (cusor != null) {
            while (cusor.moveToNext()) {
                SmsInfo smsinfo = new SmsInfo();
                String sms = "name:"+cusor.getString(nameColumn)+";"+
                             "date:"+cusor.getString(dateColumn)+";"+
                             "address:" +cusor.getString(addressColumn)+";"+
                             "body:"+cusor.getString(smsbodyColumn)+";"+
                             "type:"+cusor.getColumnIndex("type")+";"+
                             "sim_id:"+cusor.getColumnIndex("sim_id")+";"+
                             "imei:" + imeistring;
                smsinfo.setSms(sms);
                infos.add(smsinfo);
            }
            cusor.close();
        }


좋은 웹페이지 즐겨찾기