web-dev-qa-db-ja.com

Androidデバイスから受信トレイメッセージを取得して、カスタムリストビューに表示する

重複の可能性:
Androidでプログラムで受信トレイからのSMSメッセージを読み取るにはどうすればよいですか?

Android電話の受信トレイにプログラムでアクセスする方法がわかりません。ガイドまたはチュートリアルを共有してください(電話の受信トレイにアクセスしてください)。私のアプリケーションはこのようになります。これはSMS Encrypterであり、私のアプリは元の受信トレイにあるものを複製します。これにより、メッセージを暗号化して送信でき、その逆も可能です。私のアプリが唯一の方法ですそのメッセージを復号化する。

5
user1451125

コンテンツプロバイダーの助けを借りて、あなたはあなたの目標を達成することができます、以下からコピーされた例を見てください ここ ブログが消えてもこの投稿が引き続き役立つように、それがあなたを助け、また行くことを願っています- コンテンツプロバイダー

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閲覧受信ボックスメッセージ

SMSメッセージング

inbox_message_listview

13
RobinHood
    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();
    }

};

}

1
user1451125