AndroidでSMSアプリを作成しています。アドレスに基づいてメッセージの本文を暗号化したい。しかし、メッセージを受け取るとすぐに。notificationbar
明らかに私が望んでいないメッセージ本文を示しています。これを無効にして、メッセージを受信したらすぐに自分の通知を送信する方法を教えてください。
編集:
public void onReceive(final Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)){
Bundle extras = intent.getExtras();
//StringBuilder text = new StringBuilder();
if (extras != null) {
abortBroadcast();
// Get received SMS array
final Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);
// Get ContentResolver object for pushing encrypted SMS to incoming
// folder
final ContentResolver contentResolver = context.getContentResolver();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
String messages = "";
for (int i = 0; i < smsExtra.length; ++i) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
final String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();
String body1 = null;
String number = null;
Log.i("MY_SMS_RECEIVER","body address and length of extras are "+body+" "+address+" "+String.valueOf(smsExtra.length));
//Do something after 100ms
Cursor updateCursor = contentResolver.query(Uri.parse("content://sms/inbox"), new String[]{"address","body","_id","type"}, "address = ?", new String[]{address}, null);
Cursor c = contentResolver.query(Uri.parse("content://sms/inbox"), new String[]{"address","body","_id","type"}, "address = ?", new String[]{address}, null);
if(c.getCount()>0)
{
while(c.moveToNext()){
Log.i("ids in inbox content is ",String.valueOf(c.getLong(c.getColumnIndexOrThrow("_id"))));
}
}
c.close();
AddressDBHandler dba = new AddressDBHandler(context);
ContentValues val = new ContentValues();
if(dba.alreadyExisted(address)){
if(updateCursor.moveToFirst())
{
body1= updateCursor.getString(updateCursor.getColumnIndexOrThrow("body")).toString();
number = updateCursor.getString(updateCursor.getColumnIndexOrThrow("address")).toString();
Long id = updateCursor.getLong(updateCursor.getColumnIndexOrThrow("_id"));
Log.i("MY_SMS_RECEIVER", "frist received message body and address and id is "+body1+" "+number+" "+id.toString());
do
{
String status = "E";
id = updateCursor.getLong(updateCursor.getColumnIndexOrThrow("_id"));
EndecDBHandler db = new EndecDBHandler(context);
EndecDB endec = new EndecDB(id.intValue(),status);
try {
if(!db.alreadyExisted(endec)){
db.insertEncryptedData(endec);
Log.i("MY_SMS_RECEIVER","succeeded to insert");
val.put("body", SimpleCrypto.encrypt("smstrove", body1));
contentResolver.update(
Uri.parse("content://sms/inbox"), val,
"address = ? and body = ?", new String[] { address,body1 });
Log.i("MY_SMS_RECEIVER","both databases has been updated");}
}
catch(Exception e)
{
Log.i("MY_SMS_RECEIVER","could not able to insert");
}
} while(updateCursor.moveToNext());
}
updateCursor.close();
}
messages += "SMS from " + address + " :\n";
messages += body + "\n";
//putSmsToDatabase(contentResolver, sms, body,context);
}
}
}, 3000);
}
else
abortBroadcast();
// Display SMS message
// Toast.makeText(context, messages, Toast.LENGTH_LONG).show();
}
}
content://sms/inbox
更新を取得するのに必要な時間として 3 秒の遅延を設定し、暗号化されたアドレスを格納するデータベースにメッセージを送信します。問題は、インテントからデータを取得した後にブロードキャストを中止content://sms/inbox
し、新しいメッセージで更新されず、メッセージをユーザーに表示できないことです。