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

时间:2018-04-19 08:39:16

标签: java android service concurrency broadcastreceiver

  

我有service A inserts MULTIPLE_RECORDS到MULTIPLE_TABLESMULTIPLE_TABLES我稍后在another service B中使用PHONE_STATE。我有一个EXTRA_STATE_IDLE接收器,service A呼叫EXTRA_STATE_IDLE。 EveryThing工作精湛。现在开始问题:

Android 5.0行为:EXTRA_STATE_RINGING触发两次,EXTRA_STATE_OFFHOOK触发两次没有等待来电号码,TWICE-TWICE被触发service A

问题开始了,当我从EXTRA_STATE_IDLE开始DO NOT ALLOW OTHER INSTANCE TO RUN ME, IF I AM NOT FINISHED..时,我如何使服务A能够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); } } } < / p>

接收者:

@IBAction func switchTheLanguage(_ sender: UIButton) {
    var transition: UIViewAnimationOptions = .transitionFlipFromLeft

    if OHLanguage.currentAppleLanguage() == "en" {
        OHLanguage.setAppleLAnguageTo(lang: "ar")
       UIView.appearance().semanticContentAttribute = .forceRightToLeft
    } else {
        OHLanguage.setAppleLAnguageTo(lang: "en")
        transition = .transitionFlipFromRight
        UIView.appearance().semanticContentAttribute = .forceLeftToRight
    }

    let rootviewcontroller: UIWindow = ((UIApplication.shared.delegate?.window)!)!

    rootviewcontroller.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "rootnav")

    let mainwindow = (UIApplication.shared.delegate?.window!)!

    mainwindow.backgroundColor = UIColor(hue: 0.6477, saturation: 0.6314, brightness: 0.6077, alpha: 0.8)
    UIView.transition(with: mainwindow, duration: 0.55001, options: transition, animations: { () -> Void in
    }) { (finished) -> Void in

    }
}

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

1 个答案:

答案 0 :(得分:0)

  

嗯..简而言之,如果多次调用service,它会多次启动onstartcommand ...可能会并发...... Causes data loss in tables and cases in our code that is dependent on table values会导致故障。由于多个并发线程同时执行相同的代码,有点前进或后退......,请使用IntentService代替......

most Important differenceService之间的主要和IntentService,即使意外服务被调用了10次......

  1. 它将开始执行第一个请求..
  2. 如果对该IntentService进行了新的调用,那么它将继续等待...正在运行IntentService ...
  3. 当它完成第一次调用执行..时,等待中的第二个请求将开始执行IntentService。
  4. 可能最简单的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..
        }
    }