多次调用同一服务导致数据丢失

multiple calls to same service leading data loss

I have a service A, which inserts MULTIPLE_RECORDS into MULTIPLE_TABLES , that MULTIPLE_TABLES i am using later in another service B. I have a PHONE_STATE receiver, which on EXTRA_STATE_IDLE calls service A. EveryThing is working superb. Now begining of the problem :


Android 5.0 行为:EXTRA_STATE_IDLE 触发两次,EXTRA_STATE_RINGING 触发两次,没有等待呼叫号码,EXTRA_STATE_OFFHOOK 被触发 TWICE-TWICE


问题开始了,当我从 EXTRA_STATE_IDLE 开始 service A 时,我如何使服务 A 能够 DO NOT ALLOW OTHER INSTANCE TO RUN ME, IF I AM NOT FINISHED.. 你可以看到我已经尝试过 Boolean variable StateIdle


接收方:

if (action.equalsIgnoreCase("android.intent.action.NEW_OUTGOING_CALL"))
        {
            StateIdle = true;
            IsNewOutGoingCall = true;
            OutGoingPhoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.d("RECEIVER X: ", "NEW OUTGOING CALL : " + OutGoingPhoneNumber);
        }
        if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE"))
        {
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING))
            {
                StateIdle = true;
                PhoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                if(InComingNumbersList.contains(PhoneNumber))
                {
                    Log.d("RECEIVER X: ", "INCOMING NUMBER IS PRESENT IN LIST...");
                }
                else
                {
                    InComingNumbersList.add(PhoneNumber);
                    Log.d("RECEIVER X: ", "Incoming number : " + PhoneNumber);
                    Intent InsertDb = new Intent(context, CatchNumbers.class);
                    InsertDb.putExtra("TYPE", "INCOMING");
                    InsertDb.putExtra("PHONENUMBER", PhoneNumber);
                    startService(InsertDb);
                }
            }
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE))
            {
                //OutGoingPhoneNumber = "UNKNOWN";
                InComingNumbersList.clear();
                if(StateIdle)
                {
                    StateIdle = false;
                    Log.d("RECEIVER X: ", "CALL ENDED... ");
                    Intent SendSMS = new Intent(context, SendSMS.class);
                    startService(SendSMS);
                }
            }
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
            {
                if(IsNewOutGoingCall)
                {
                    IsNewOutGoingCall = false;
                    Log.d("RECEIVER X: ", "ACTIVE OUTGOING CALL : ");
                    Intent InsertDb = new Intent(context, CatchNumbers.class);
                    InsertDb.putExtra("TYPE", "OUTGOING");
                    InsertDb.putExtra("PHONENUMBER", OutGoingPhoneNumber);
                    startService(InsertDb);
                }

            }
        }

提前谢谢你,希望有人能帮助我...:)

Well.., In short and sweet, If a service gets called multiple times, it starts onstartcommand multiple times... May be concurrent... Causes data loss in tables and cases in our code that is dependent on table values causes malfunction. As multiple concurrent threads executes same code in same time, a little ahead or back.., Use IntentService instead...


Main 和most Important difference 介于ServiceIntentService 之间,即使Intent Service 被调用10 次...

  1. 它将开始执行第一个请求..
  2. 如果对该 IntentService 进行了新的调用,那么它将继续等待...因为 IntentService 正在 运行...
  3. 当它完成第一个调用执行...时,等待中的第二个请求将开始执行 IntentService。

可能最简单的 IntentService:

public class MyWorkerThread extends IntentService
{

    public MyWorkerThread()
    {
        super("SMSEngine");
        Log.d("MyWorkerThread : ", "\nCreated...");
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        // Your background code goes here..
    }
}