从android设备获取收件箱消息以显示在自定义列表视图中



可能重复:
如何在Android中以编程方式从收件箱中读取短信?

我不知道我如何以编程方式访问android手机的收件箱,你能指导我或分享一些我如何做到这一点的教程吗(访问手机的收件箱(。顺便说一下,我的申请是这样的。它是一个短信加密程序,我的应用程序复制了原始收件箱中的内容,这样我就可以在发送时加密消息,反之亦然,我的程序是解密该消息的唯一方法。

在内容提供商的帮助下,你可以实现你的目标,看看下面的例子,它是从这里复制的,这样如果博客消失了,这篇文章仍然有用,希望它能帮助你,也能通过内容提供商。

AndroidManifest.xml

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itcuties.android.apps"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
       android:minSdkVersion="8"
       android:targetSdkVersion="16" />
    <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.itcuties.android.apps.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>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000">
    <TextView
       android:id="@+id/smsNumberText"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="NUMBER_GOES_HERE">
    </TextView>
</LinearLayout>

SMSData.java

package com.itcuties.android.apps.data;
/**
* This class represents SMS.
*
* @author itcuties
*
*/
public class SMSData {
   // Number from witch the sms was send
   private String number;
   // SMS text body
   private String body;
   public String setNumber() {
       return number;
   }
   public void setNumber(String number) {
       this.number = number;
   }
   public String setBody() {
       return body;
   }
   public void setBody(String body) {
       this.body = body;
   }
}

ListAdapter.java

package com.itcuties.android.apps;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.itcuties.android.apps.data.SMSData;

/**
* List adapter for storing SMS data
*
* @author itcuties
*
*/
public class ListAdapter extends ArrayAdapter<SMSData> {
   // List context
    private final Context context;
    // List values
    private final List<SMSData> smsList;
   public ListAdapter(Context context, List<SMSData> smsList) {
       super(context, R.layout.activity_main, smsList);
       this.context = context;
       this.smsList = smsList;
   }
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.activity_main, parent, false);
       TextView senderNumber = (TextView) rowView.findViewById(R.id.smsNumberText);
       senderNumber.setText(smsList.get(position).setNumber().toString());
       return rowView;
   }
}

MainActivity.java

package com.itcuties.android.apps;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.itcuties.android.apps.data.SMSData;
/**
* Main Activity. Displays a list of numbers.
*
* @author itcuties
*
*/
public class MainActivity extends ListActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
 ArrayList<Message> smsInbox = new ArrayList<Message>();
Uri uriSms = Uri.parse("content://sms");
Cursor cursor = this.getContentResolver()
        .query(uriSms,
                new String[] { "_id", "address", "date", "body",
                        "type", "read" }, "type=" + type, null,
                "date" + " COLLATE LOCALIZED ASC");
if (cursor != null) {
    cursor.moveToLast();
    if (cursor.getCount() > 0) {
        do {
            Message message = new Message();
            message.messageNumber = cursor.getString(cursor
                    .getColumnIndex("address"));
            message.messageContent = cursor.getString(cursor
                    .getColumnIndex("body"));
            smsInbox.add(message);
        } while (cursor.moveToPrevious());
    }
       }
       c.close();
       // Set smsList in the ListAdapter
       setListAdapter(new ListAdapter(this, smsList));
   }
   @Override
   protected void onListItemClick(ListView l, View v, int position, long id) {
       SMSData sms = (SMSData)getListAdapter().getItem(position);
       Toast.makeText(getApplicationContext(), sms.setBody(), Toast.LENGTH_LONG).show();
   }
}

也检查以下链接相关的@

Android阅读收件箱消息

短信

inbox_message_listview

    public class MainActivity extends Activity {
private static final int TYPE_INCOMING_MESSAGE = 1;
private ListView messageList;
private MessageListAdapter messageListAdapter;
private ArrayList<Message> recordsStored;
private ArrayList<Message> listInboxMessages;
private ProgressDialog progressDialogInbox;
private CustomHandler customHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initViews();
}
@Override
public void onResume() {
    super.onResume();
    populateMessageList();
}
private void initViews() {
    customHandler = new CustomHandler(this);
    progressDialogInbox = new ProgressDialog(this);
    recordsStored = new ArrayList<Message>();
    messageList = (ListView) findViewById(R.id.messageList);
    populateMessageList();
}
public void populateMessageList() {
    fetchInboxMessages();
    messageListAdapter = new MessageListAdapter(this,
            R.layout.message_list_item, recordsStored);
    messageList.setAdapter(messageListAdapter);
}
private void showProgressDialog(String message) {
    progressDialogInbox.setMessage(message);
    progressDialogInbox.setIndeterminate(true);
    progressDialogInbox.setCancelable(true);
    progressDialogInbox.show();
}
private void fetchInboxMessages() {
    if (listInboxMessages == null) {
        showProgressDialog("Fetching Inbox Messages...");
        startThread();
    } else {
        // messageType = TYPE_INCOMING_MESSAGE;
        recordsStored = listInboxMessages;
        messageListAdapter.setArrayList(recordsStored);
    }
}
public class FetchMessageThread extends Thread {
    public int tag = -1;
    public FetchMessageThread(int tag) {
        this.tag = tag;
    }
    @Override
    public void run() {
        recordsStored = fetchInboxSms(TYPE_INCOMING_MESSAGE);
        listInboxMessages = recordsStored;
        customHandler.sendEmptyMessage(0);
    }
}
public ArrayList<Message> fetchInboxSms(int type) {
    ArrayList<Message> smsInbox = new ArrayList<Message>();
    Uri uriSms = Uri.parse("content://sms");
    Cursor cursor = this.getContentResolver()
            .query(uriSms,
                    new String[] { "_id", "address", "date", "body",
                            "type", "read" }, "type=" + type, null,
                    "date" + " COLLATE LOCALIZED ASC");
    if (cursor != null) {
        cursor.moveToLast();
        if (cursor.getCount() > 0) {
            do {
                Message message = new Message();
                message.messageNumber = cursor.getString(cursor
                        .getColumnIndex("address"));
                message.messageContent = cursor.getString(cursor
                        .getColumnIndex("body"));
                smsInbox.add(message);
            } while (cursor.moveToPrevious());
        }
    }
    return smsInbox;
}
private FetchMessageThread fetchMessageThread;
private int currentCount = 0;
public synchronized void startThread() {
    if (fetchMessageThread == null) {
        fetchMessageThread = new FetchMessageThread(currentCount);
        fetchMessageThread.start();
    }
}
public synchronized void stopThread() {
    if (fetchMessageThread != null) {
        Log.i("Cancel thread", "stop thread");
        FetchMessageThread moribund = fetchMessageThread;
        currentCount = fetchMessageThread.tag == 0 ? 1 : 0;
        fetchMessageThread = null;
        moribund.interrupt();
    }
}
static class CustomHandler extends Handler {
    private final WeakReference<MainActivity> activityHolder;
    CustomHandler(MainActivity inboxListActivity) {
        activityHolder = new WeakReference<MainActivity>(inboxListActivity);
    }
    @Override
    public void handleMessage(android.os.Message msg) {
        MainActivity inboxListActivity = activityHolder.get();
        if (inboxListActivity.fetchMessageThread != null
                && inboxListActivity.currentCount == inboxListActivity.fetchMessageThread.tag) {
            Log.i("received result", "received result");
            inboxListActivity.fetchMessageThread = null;
            inboxListActivity.messageListAdapter
                    .setArrayList(inboxListActivity.recordsStored);
            inboxListActivity.progressDialogInbox.dismiss();
        }
    }
}
private OnCancelListener dialogCancelListener = new OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        stopThread();
    }
};

}

相关内容

  • 没有找到相关文章

最新更新