我正在开发一个应用,其中应用必须显示接收消息以#
开头。现在我必须显示他们的通知,我还想在ListView
内的Activity
内更新通知。目前,我使用ContentResolver
从收件箱中收到邮件。因为一旦处理过的消息仍然显示在活动中。我不想表明这一点。我希望我收到的消息通知以活动中#
中列出的ListView
开头。
我的代码是
我的活动
public class ActivationActivity extends ActionBarActivity implements OnItemClickListener {
private static ActivationActivity inst;
ArrayList<String> smsMessagesList = new ArrayList<String>();
ListView smsListView;
ArrayAdapter arrayAdapter;
Context context;
public static ActivationActivity instance() {
return inst;
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activation);
smsListView=(ListView)findViewById(R.id.listView);
arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,smsMessagesList);
smsListView.setAdapter(arrayAdapter);
refreshSmsInbox();
smsListView.setOnItemClickListener(this);
}
public void refreshSmsInbox(){
ContentResolver contentResolver=getContentResolver();
final Cursor smsInboxCursor=contentResolver.query(Uri.parse("content://sms/inbox"),null,null,null,null);
int indexBody=smsInboxCursor.getColumnIndex("body");
int indexAddress=smsInboxCursor.getColumnIndex("address");
if (indexBody<0||!smsInboxCursor.moveToFirst()) return;
arrayAdapter.clear();
do {
if (smsInboxCursor.getString(smsInboxCursor.getColumnIndex("body")).startsWith("#")) {
String str = smsInboxCursor.getString(indexAddress)+ smsInboxCursor.getString(indexBody)+"\n";
arrayAdapter.add(str);
}else {
}
} while (smsInboxCursor.moveToNext());
}
public void updateList(final String smsMessage) {
arrayAdapter.insert(smsMessage, 0);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try{
String[] smsMessages=smsMessagesList.get(position).split("#");
String address=smsMessages[0];
String dCode=smsMessages[1];
String user=smsMessages[2];
String addrs1=smsMessages[3];
String pin=smsMessages[4];
String addrs=addrs1+","+pin;
String sNo=smsMessages[5];
databaseHelper=new DatabaseHelper(this);
SQLiteDatabase db=databaseHelper.getReadableDatabase();
SmsManager smsManager=SmsManager.getDefault();
Cursor cursor= db.rawQuery("select uSerialNo,dealerCode from unittable", null);
cursor.moveToFirst();
String srNo=cursor.getString(0);
String drCode=cursor.getString(1);
if (srNo.equals(sNo) || drCode.equals(dCode)){
ContentValues cv=new ContentValues();
cv.put("userName",user);
cv.put("mobileNo", address);
cv.put("address", addrs);
db.update("unittable", cv, "uSerialNo='" + srNo + "'", null);
db.update("unittable", cv, "uSerialNo='" + srNo + "'", null);
db.update("unittable", cv, "uSerialNo+'" + srNo + "'", null);
Toast.makeText(ActivationActivity.this,"Updation Success",Toast.LENGTH_LONG).show();
smsManager.sendTextMessage(address,null,"YES. ACTIVATE. Default Password is 1234",null,null);
} else {
Toast.makeText(ActivationActivity.this,"Failed",Toast.LENGTH_LONG).show();
smsManager.sendTextMessage(address,null,"NO. Details or Incorrect. Check the details or Call the Customer Care",null,null);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
我的BroadcastReceiver是
public class SmsBroadcastReceiver extends BroadcastReceiver {
public static final String smsBundle="pdus";
public void onReceive(Context context, Intent intent){
Bundle intentExtras=intent.getExtras();
if (intentExtras!=null){
Object[] sms=(Object[])intentExtras.get(smsBundle);
String SmsManager="";
for (int i = 0; i < sms.length; i++) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String smsBody = smsMessage.getMessageBody().toString();
String smsAddress = smsMessage.getOriginatingAddress();
SmsManager += "SMS From: " + smsAddress + "\n";
SmsManager += smsBody + "\n";
}
Toast.makeText(context, SmsManager, Toast.LENGTH_LONG).show();NotificationCompat.Builder(this);
ActivationActivity inst = ActivationActivity.instance();
inst.updateList(SmsManager);
}
}
}