我有
service A
inserts
MULTIPLE_RECORDS到MULTIPLE_TABLES
,MULTIPLE_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
}
}
提前谢谢你,希望有人帮助我... :)
答案 0 :(得分:0)
嗯..简而言之,如果多次调用
service
,它会多次启动onstartcommand
...可能会并发......Causes data loss in tables and cases in our code that is dependent on table values
会导致故障。由于多个并发线程同时执行相同的代码,有点前进或后退......,请使用IntentService
代替......
most Important difference
和Service
之间的主要和IntentService
,即使意外服务被调用了10次......
可能最简单的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..
}
}