【发布时间】:2020-01-13 15:29:40
【问题描述】:
您好,我需要有关 if 语句的帮助,它总是在检查条件之前跳转到 else ! 我的代码:
@Override
public void onReceive(Context p1, Intent p2)
{
if ("android.intent.action.BOOT_COMPLETED".equals(p2.getAction())) {
Toast.makeText(p1, "Device is ON from Recever",Toast.LENGTH_LONG).show();
Intent starServiceIntent = new Intent(p1,MyService.class);
p1 .startService(starServiceIntent);
} @Override
public void onReceive(Context p1, Intent p2)
{
if ("android.intent.action.BOOT_COMPLETED".equals(p2.getAction())) {
Toast.makeText(p1, "Device is ON from Recever",Toast.LENGTH_LONG).show();
Intent starServiceIntent = new Intent(p1,MyService.class);
p1 .startService(starServiceIntent);
} if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
SharedPreferences prefs = p1.getSharedPreferences("myPrefss", Context.MODE_PRIVATE);
if(!prefs.contains(incomingNumber)){
Toast.makeText(p1,"True",Toast.LENGTH_SHORT).show();
DateFormat timeFormat = new SimpleDateFormat("dd-MM-yy HH:mm");
timeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Jerusalem"));
String curTime = timeFormat.format(new Date());
SharedPreferences.Editor editor = prefs.edit();
editor.putString(incomingNumber, curTime);
editor.commit();
sendSMS(incomingNumber,p1);
}else{
getStored(p1,incomingNumber);
Toast.makeText(p1,"False",Toast.LENGTH_SHORT).show();
}
}
//先给我假再给真,即使条件满足
【问题讨论】:
-
如果去else,则条件为假。仔细检查您的数据。
-
先给我假然后真,但为什么先给我假!
标签: android service broadcastreceiver