我已经开发了这个简单的应用程序来收听我的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