zoukankan      html  css  js  c++  java
  • 16 读取短信内容

    • 所需权限

        <!-- 读取短信的权限 -->
          <uses-permission android:name="android.permission.READ_SMS"/>
    • 布局Activity :

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.qf.day16_contentresolver_sms_demo1"
          android:versionCode="1"
          android:versionName="1.0" >
      
          <uses-sdk
              android:minSdkVersion="8"
              android:targetSdkVersion="18" />
          <!-- 读取短信的权限 -->
          <uses-permission android:name="android.permission.READ_SMS"/>
      
          <application
              android:allowBackup="true"
              android:icon="@drawable/ic_launcher"
              android:label="@string/app_name"
              android:theme="@style/AppTheme" >
              <activity
                  android:name="com.qf.day16_contentresolver_sms_demo1.MainActivity"
                  android:label="@string/app_name" >
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
      
                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
          </application>
      
      </manifest>
      
    • java代码(MainActivity)

      package com.qf.day16_contentresolver_sms_demo1;
      
      import android.net.Uri;
      import android.os.Bundle;
      import android.annotation.SuppressLint;
      import android.app.Activity;
      import android.content.ContentResolver;
      import android.database.Cursor;
      import android.view.Menu;
      import android.widget.ListView;
      import android.widget.SimpleCursorAdapter;
      
      /**
       * 使用ContentResolver获取数据步骤:
       * 1,获取ContentResolver对象
       * 2,使用ContentResolver 查询数据,  得到Uri对象
       * 3,将数据展示到ListVIew
       *
       */
      public class MainActivity extends Activity {
      
          //内容解析器
          private ContentResolver contentResolver;
          //Uri  访问私有数据的路径
          private  Uri smsUri = Uri.parse("content://sms");
      
          private ListView lv;
      
          private SimpleCursorAdapter adapter;
      
          @SuppressLint("NewApi")
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
      
               lv = (ListView) findViewById(R.id.lv);
              //获取实例化对象
              contentResolver = getContentResolver();
      
              /**
               * 获取数据  通过Uri
               * 
               * 参数1:访问ContentProvider 的路径
               * 参数2:查询的字段项       null代表查询所有
               * 参数3:  查询的条件  _id =?  body = ?
               * 参数4:  查询条件的占位符
               * 参数5: 排序
               *  SimpleCursorAdapter 必须要查找_id字段 哪怕你不使用
               */
              Cursor cursor = contentResolver.query(smsUri, null, null, null, null);
      
              adapter = new SimpleCursorAdapter(MainActivity.this,
                      R.layout.item, 
                      cursor, new String[]{"address","body"}, 
                      new int[]{R.id.tv_address,R.id.tv_body},
                      SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
              lv.setAdapter(adapter);
          }
      
      
      
      }
      
    • layout下布局文件
      activity_main.xml(Activity界面)

      <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context=".MainActivity" >
      
          <ListView 
              android:id="@+id/lv"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              ></ListView>
      
      </RelativeLayout>
      

      list填充的布局

      <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context=".MainActivity" >
      
          <ListView 
              android:id="@+id/lv"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              ></ListView>
      
      </RelativeLayout>
      
  • 相关阅读:
    Apple Watch知识点总结
    segue场景跳转的使用总结
    iOS地图相关知识点总结
    第三方库AFNetwork的作用和用法详解
    UIImagePickerController的知识点总结
    关于图片的压缩问题
    盒子模型知识
    CSS3新增属性
    ps命令详解
    http请求中的Referer的作用
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152244.html
Copyright © 2011-2022 走看看