簡體   English   中英

如何以編程方式從Android檢索未讀消息

[英]How to retrieve the unread messages from Android programmatically

這是我的代碼。 它將允許我檢索從我的Android手機發送的所有消息。 我只想檢索未讀的郵件。

提前致謝。

private String load_sent_sms() {
        // TODO Auto-generated method stub
        Uri uriSms = Uri.parse("content://sms/sent");
        Cursor cursor = getContentResolver().query(uriSms,
                new String[] { "_id", "address", "date", "body" }, null, null,
                null);
        String sms = "";
        cursor.moveToFirst();
        while (cursor.moveToNext()) {
            String address = cursor.getString(1);
            String body = cursor.getString(3);
            sms += "Contact Name: " + address + "\nMessage: " + body + "\n";

        }
        return sms;

    }
main.xml:

package com.example.esemesy;

    import java.util.ArrayList;


    import android.support.v7.app.ActionBarActivity;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;

    public class MainActivity extends ActionBarActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);




             final ListView lViewSMS = (ListView) findViewById(R.id.listViewSMS);


                if(fetchInbox()!=null)
                {
                    @SuppressWarnings("unchecked")
                    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, fetchInbox());
                    lViewSMS.setAdapter(adapter);
                }


        }

        ArrayList fetchInbox (){

            final Uri SMS_INBOX = Uri.parse("content://sms/inbox");

            //Retrieves all SMS (if you want only unread SMS, put "read = 0" for the 3rd parameter)
            Cursor cursor = getContentResolver().query(SMS_INBOX, null, "read=0", null, null);
            ArrayList sms = new ArrayList();
            //Get all lines  
            while (cursor.moveToNext()) {
                            //Gets the SMS information
                            String address = cursor.getString(cursor.getColumnIndex("address"));
                            String person = cursor.getString(cursor.getColumnIndex("person")); 
                            String date = cursor.getString(cursor.getColumnIndex("date")); 
                            String protocol = cursor.getString(cursor.getColumnIndex("protocol")); 
                            String read = cursor.getString(cursor.getColumnIndex("read")); 
                            String status = cursor.getString(cursor.getColumnIndex("status")); 
                            String type = cursor.getString(cursor.getColumnIndex("type")); 
                            String subject = cursor.getString(cursor.getColumnIndex("subject")); 
                            String body = cursor.getString(cursor.getColumnIndex("body"));

                            sms.add(address+"\n"+body);
                            //Do what you want
            }
             return sms;
        }


        }
mainlayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.esemesy.MainActivity"
    tools:ignore="MergeRootFrame" >

    <ListView
        android:id="@+id/listViewSMS"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</FrameLayout>


manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.esemesy"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.READ_SMS">
</uses-permission>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.esemesy.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>

資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM