2015-10-19 48 views
0

我已经开发了这个简单的应用程序来收听我的Android手机收到的短信。即使条件不满足,为什么会调用应用程序?

正如您在代码中看到的那样,当SMS到达时,我会扫描其内容以查找特定文本,以便在不满足此条件的情况下不做任何事情。

真正的问题是,当SMS到达并且条件不满足时,创建的用于控制何时调用“地图”应用程序的IF语句不会被执行,但“地图”应用程序被调用方式,并加载与符合条件的最后一条SMS相关的信息。

该代码完美地在我的模拟器中使用KitKat,但是在我的手机中使用Kitkat 4.4.2正在处理我刚写入的行为。

你以前遇到过这样的事情吗? 你能否告诉我一些解决这个问题的方法?

这是主要的活性

public class MainActivity extends ActionBarActivity { 
EditText et_location_data; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button bt_manual = (Button) findViewById(R.id.bt_manual); 
    et_location_data = (EditText) findViewById(R.id.et_location_data); 

    bt_manual.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String messageBody = ""; 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
       messageBody = et_location_data.getText().toString(); 
       Common_Functions common_functions = new Common_Functions(getBaseContext(), messageBody); 
       common_functions.show(); 
      } 
     } 
    }); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    et_location_data.setText(""); 
}} 

这是广播监听

public class SMS_listener extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.getAction())) { 
     String messageBody= ""; 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {Telephony.Sms.Intents.getMessagesFromIntent(intent)) {SmsMessage smsMessage = Telephony.Sms.Intents.getMessagesFromIntent(intent)[0]; 
       messageBody = smsMessage.getMessageBody(); 
      Common_Functions common_functions = new Common_Functions(context, messageBody); 
      common_functions.show(); 
     } 
     Log.d("Test", "Message body: " + messageBody); 
    } 
}} 

这是一个一般用途的类代码重用

public class Common_Functions { 
String messageBody= ""; 
String STR_CODE_LATITUDE = "Latitude"; 
String STR_CODE_LONGITUDE = "Longitude"; 
Context context; 

String str_Lat = ""; 
String str_Lon = ""; 

public Common_Functions(Context context, String messageBody) { 
    this.context = context; 
    this.messageBody = messageBody; 
} 

public void show() { 
    if (messageBody.toLowerCase().contains("Latitud".toLowerCase())) { 
     int i = messageBody.toLowerCase().indexOf(STR_CODE_LATITUDE.toLowerCase()); 
     int j = messageBody.toLowerCase().indexOf(STR_CODE_LONGITUDE.toLowerCase()); 

     str_Lat = messageBody.substring(i + 1 + STR_CODE_LATITUDE.length(), i + 1 + STR_CODE_LATITUDE.length() + 10); 
     str_Lon = messageBody.substring(j + 1 + STR_CODE_LONGITUDE.length(), j + 1 + STR_CODE_LONGITUDE.length() + 11); 

     if (str_Lat.toLowerCase().contains("s")) 
      str_Lat = "-" + messageBody.substring(i + 1 + STR_CODE_LATITUDE.length(), i + 1 + STR_CODE_LATITUDE.length() + 9); 
     else 
      str_Lat = messageBody.substring(i + 1 + STR_CODE_LATITUDE.length(), i + 1 + STR_CODE_LATITUDE.length() + 9); 

     if (str_Lon.toLowerCase().contains("w")) 
      str_Lon = "-" + messageBody.substring(j + 1 + STR_CODE_LONGITUDE.length(), j + 1 + STR_CODE_LONGITUDE.length() + 10); 
     else 
      str_Lon = messageBody.substring(j + 1 + STR_CODE_LONGITUDE.length(), j + 1 + STR_CODE_LONGITUDE.length() + 10); 

     Intent intent1 = new Intent(android.content.Intent.ACTION_VIEW, 
       Uri.parse("http://maps.google.com/maps?q=" + str_Lat + "," + str_Lon)); 
     context.startActivity(intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); 

    } 
}} 

这是清单

<?xml version="1.0" encoding="utf-8"?> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_action" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <receiver android:name=".SMS_listener"> 
     <intent-filter android:priority="200"> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
     </intent-filter> 
    </receiver> 

</application> 
<uses-permission android:name="android.permission.RECEIVE_SMS" /> 

+0

那么你说你不应该一直有这个意图吗?因为目前那是什么发生你有什么 – tyczj

+0

是的,这是正确的。只有在SMS包含文字“纬度”时才应该调用意图。而且只有当满足这个条件时才会调用它,但我不知道为什么“地图”应用程序仍然被调用,即使条件不满足。 – RunningWheels

+0

当你评论地图意图时会发生什么? – tyczj

回答

0

我发现我是用这个应用程序面临的问题。

因为我在做这个张贴在这里的最终代码之前已经做了几个测试,所以我的手机似乎在缓存中存储了一些奇怪的信息,特别是“消息传递”过程。

我意识到这一点,当我完全卸载应用程序,然后发送短信到我的手机。令人惊讶的是,即使卸载了应用程序,“地图”应用程序仍在被调用。这给了我清除“消息传递”过程和Eureka缓存的想法,问题解决了。

未来的经验。但是,这种行为似乎是我的Android 4.4.2版本的一大缺陷,因为我无法想象这个操作系统无法管理这种威胁。