0

在我的应用程序中,我将收到短信。我的代码正在运行,没有崩溃。但是这些消息没有显示在我的应用程序收件箱列表视图中。我不希望将消息存储在我的应用程序中。

收件箱

  public class InboxQuick extends Activity  implements OnClickListener,
  OnItemClickListener{

private ListView smsListView;
private BaseAdapter smsAdapter;
private ArrayList<String> msgList = new ArrayList<String>();
private boolean isAdHocQuestions;
// received msgs
public static Map<String, String> smsCache = new HashMap<String, String>();
public static int type;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inboxquick);

    Button summary = (Button) findViewById(R.id.btnsummaryquick);
    summary.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent("sg.edu.tp.iit.mns.SummaryQuick");
            intent.putExtra("showQuestion", true);
            startActivity(intent);
        }
    });

    isAdHocQuestions = getIntent().getBooleanExtra("type", false);

    smsListView = (ListView) findViewById(R.id.msgList);

    // buildSmsList();
    smsAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, msgList);
    smsListView.setAdapter(smsAdapter);
    smsListView.setOnItemClickListener(this);
}

private void unRegistSmsReceiver() {
    unregisterReceiver(inboxSmsReveiver);
}

private BroadcastReceiver inboxSmsReveiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.provider.Telephony.SMS_RECEIVED".equals(intent
                .getAction())) {
            Object[] pdus = (Object[]) intent.getExtras().get("pdus");
            for (int i = 0; i < pdus.length; i++) {
                SmsMessage message = SmsMessage
                        .createFromPdu((byte[]) pdus[i]);
                String body = message.getDisplayMessageBody();
                if (body.length() != 0 && body.startsWith("##")) {
                    body = body.replaceAll("\n", "");
                    String[] answerBody = body.split("##");
                    if (answerBody.length == 3) {
                        String answer = answerBody[1];
                        String fromName = answerBody[2];
                        // if msg is send by same student (by name) then msg
                        // will not be counted
                        if (!smsCache.containsKey(fromName)) {

                            // save txt msg
                            type = "ABCD".indexOf(answer) == -1 ? 1 : 2;
                            String saveBody = answer;
                            saveNewSms(fromName, saveBody);

                            // msg display
                            String showBody = "name=" + fromName
                                    + ",answer=" + answer;
                            updateSmsList(showBody);

                        }
                    } else {
                        // TODO error msg
                    }
                } else {
                    continue;
                }
            }
        }
    }
};

private void saveNewSms(String key, String body) {
    smsCache.put(key, body);
}

private void updateSmsList(String body) {
    TextView title = (TextView) findViewById(R.id.tv);
    title.setText("Inbox(" + smsCache.size() + ")");
    msgList.add(body);
    smsAdapter.notifyDataSetChanged();
}

@Override
protected void onPause() {
    super.onPause();
    if (isAdHocQuestions)
        unRegistSmsReceiver();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

短信接收器

    public void onReceive( Context context, Intent intent ) 
{
    // Get SMS map from Intent
    Bundle extras = intent.getExtras();

    String messages = "";

    if ( extras != null )
    {
        // Get received SMS array
        Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );

        for ( int i = 0; i < smsExtra.length; ++i )
        {
            SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);

            String body = sms.getMessageBody().toString();
            String address = sms.getOriginatingAddress();           

            messages += "SMS from " + address + " :\n";     
            messages += body + "\n";

            // Here you can add any your code to work with incoming SMS         
            putSmsToDatabase( sms );
        }

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor edit = prefs.edit();
        edit.putBoolean("need_update", true);
        edit.commit();

        // Display SMS message
        Toast.makeText(context, messages, Toast.LENGTH_SHORT).show();

    }

}

private void putSmsToDatabase( SmsMessage sms )
{
    // Create SMS row
    ContentValues values = new ContentValues();
    values.put( ADDRESS, sms.getOriginatingAddress() );
    values.put( READ, MESSAGE_IS_NOT_READ );
    values.put( SEEN, MESSAGE_IS_NOT_SEEN );
}
4

0 回答 0