2

イベントをリッスンし、android.provider.Telephony.SMS_RECEIVED独自の通知を作成するブロードキャスト レシーバーを作成しました。

また、アプリで同じレシーバーを使用して、コールバックを使用して SMS を受信したときにアクティビティを更新します。

問題は、SMS アプリ通知イベントが通知後にディスパッチされるため、更新時に SMS が content://sms に事前設定されていないことです。

可能であれば通知を遅らせたいのですが、方法がわかりません。

コードは次のとおりです。

public class SmsReceiver extends BroadcastReceiver {

Context context;

int nmessages = 0;


@Override
public void onReceive(Context context, Intent intent) {
//—get the SMS message passed in—
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String messages = "";

this.context = context;

if (bundle != null)
{
    //—retrieve the SMS message received—
    Object[] smsExtra = (Object[]) bundle.get("pdus");

    msgs = new SmsMessage[smsExtra.length];

    nmessages = SmsHolder.getNumberUnreadSms(context) +1;


    for (int i=0; i<msgs.length; i++)
    {
        SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
        //take out content from sms
        String body = sms.getMessageBody().toString();
        String address = sms.getOriginatingAddress();

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

        putSmsToDatabase(sms, context );
    }
    //—display the new SMS message—

    createNotification(SmsMessage.createFromPdu((byte[])smsExtra[0]), context);

    updateActivity();
}
}

public void updateActivity(){

}

private void putSmsToDatabase( SmsMessage sms, Context context )
{



    String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
    // Create SMS row
    ContentValues values = new ContentValues();

    values.put("address", sms.getOriginatingAddress().toString() );
    values.put("date", mydate);
    values.put("body", sms.getMessageBody().toString());
    // values.put( READ, MESSAGE_IS_NOT_READ );
    // values.put( STATUS, sms.getStatus() );
    // values.put( TYPE, MESSAGE_TYPE_INBOX );
    // values.put( SEEN, MESSAGE_IS_NOT_SEEN );



}

private void createNotification(SmsMessage sms, Context context){

    NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    String contentTitle;
     if (nmessages < 2){
         contentTitle = "SMS: " + ContactsInterface.getContactDisplayNameByNumber(sms.getOriginatingAddress(), context);
     }else {
         contentTitle = nmessages + " " + context.getResources().getString(R.string.new_messages);
     }

    // construct the Notification object.
        NotificationCompat.Builder  builder = new NotificationCompat.Builder(context)
        .setContentTitle(contentTitle)
         .setContentText(sms.getMessageBody())
         .setSmallIcon(R.drawable.ic_launcher)
         .setLargeIcon(getIconBitmap())
         .setNumber(nmessages);

        builder.setAutoCancel(true);

        //(R.drawable.stat_sample, tickerText,
          //      System.currentTimeMillis());

        // Set the info for the views that show in the notification panel.
        //notif.setLatestEventInfo(this, from, message, contentIntent);
        /*
        // On tablets, the ticker shows the sender, the first line of the message,
        // the photo of the person and the app icon.  For our sample, we just show
        // the same icon twice.  If there is no sender, just pass an array of 1 Bitmap.
        notif.tickerTitle = from;
        notif.tickerSubtitle = message;
        notif.tickerIcons = new Bitmap[2];
        notif.tickerIcons[0] = getIconBitmap();;
        notif.tickerIcons[1] = getIconBitmap();;
        */

     // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(context, Login.class);

        // Because clicking the notification opens a new ("special") activity, there's
        // no need to create an artificial back stack.
        PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
            context,
            0,
            resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT
        );


       // Ritardo in millisecondi



     builder.setContentIntent(resultPendingIntent);


        // Note that we use R.layout.incoming_message_panel as the ID for
        // the notification.  It could be any integer you want, but we use
        // the convention of using a resource id for a string related to
        // the notification.  It will always be a unique number within your
        // application.
        nm.notify(R.drawable.ic_drawer, builder.build());
}




private Bitmap getIconBitmap() {
    BitmapFactory f = new BitmapFactory();
    return f.decodeResource(context.getResources(), R.drawable.ic_sms);
}

}

4

1 に答える 1