0

In my Android application, when a receive a SMS my broacast start. I want to manage when i receive a new SMS when the first one is not finish. I have idea kill the first proccess. But i don't no what to do it. If you can help me. I'm biginner in Android. There is my Brocast code.

Thanks.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.text.TextUtils;

public class SMSReceiver extends BroadcastReceiver {
private static final String number = "15555215556";

@Override
public void onReceive(Context context, Intent intent) {

    Bundle bundle = intent.getExtras();
    Object[] messages = (Object[]) bundle.get("pdus");
    SmsMessage[] sms = new SmsMessage[messages.length];

    // Creation
    for (int i=0; i < messages.length; i++) {
        sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
    }
    for (SmsMessage msg : sms) {

        // Vérifie
        if (TextUtils.equals(msg.getOriginatingAddress(), number)) {
            Intent i = new Intent(context, DisplayActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            String sms2 = msg.getMessageBody();
            i.putExtra("SMS",sms2);
            context.startActivity(i);

            abortBroadcast();
        }
    }

}
}
Merv
  • 55
  • 1
  • 2
  • 8

1 Answers1

1

I want to manage when i receive a new SMS when the first one is not finish.

This is not quite clear to me, you should not do any heavy work in your broadcast, you better copy necessary data to ie. shared preferences or database, and start IntentService that will process it further. With IntentService you can pass your data to service directly in Intent.

But from your code I dont see any long processing

kill the first proccess

That is always a bad idea under android (look here for longer explanation: Is quitting an application frowned upon?). Also is abortBroadcast(); your intention? to suppress notifications?

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • I see,thanks. when my brodcast start, music play : context.startActivity(i); my problem is if have 2 sms simultaneously the music is mixed. you see? – Merv Jan 16 '14 at 17:12
  • Not sure what is the problem, if you want only single activity, then use FLAG_ACTIVITY_SINGLE_TOP flag when starting activity. Your activity will receive new intent in 'void onNewIntent '. – marcinj Jan 16 '14 at 17:18
  • I think following should work to make your activity single instance: during intent creation: `Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK`, and in manifest for your activity: `android:launchMode="singleTask"`. – marcinj Jan 16 '14 at 17:28
  • thanks for your answer. it's like that but there is an important probleme. when i a SMS arrives i save it in my database sqlite. they are now a problem. when the second sms arrives he not bz saved. but the first saved. have you an idea? Please – Merv Jan 16 '14 at 17:45
  • thanks for your help. if i can stop the first task and start the new – Merv Jan 16 '14 at 18:05