我已經開發了這個簡單的應用程序來收聽我的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" />
那麼你說你不應該一直有這個意圖嗎?因爲目前那是什麼發生你有什麼 – tyczj
是的,這是正確的。只有在SMS包含文字「緯度」時才應該調用意圖。而且只有當滿足這個條件時纔會調用它,但我不知道爲什麼「地圖」應用程序仍然被調用,即使條件不滿足。 – RunningWheels
當你評論地圖意圖時會發生什麼? – tyczj