我的短信接收器存在一些问题。应用程序第一次运行。只要我不关闭应用程序,所有消息都会显示在我的列表视图中。
如果我关闭我的应用程序,第二次它表现得很奇怪。通常只显示第一条消息。当第一条消息“到达”应用程序时,内置的短信应用程序不会显示消息的预览。在第二条消息和转发上,将显示内置应用程序中的消息。 (我同时接收和发送来自同一部电话的消息,如果从其他电话收到消息则会出现同样的问题)
如果显示多条消息,则为第一条消息的副本。
我有两个班级;
package com.example.t.receivesms;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
static ArrayList<String> messageList=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> mAdapter;
mAdapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, messageList);
ListView listView=findViewById(R.id.myListView);
listView.setAdapter(mAdapter);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String address = extras.getString("MessageNumber");
String message = extras.getString("Message");
String conCatString=("From:"+address +"\n"+ message);
messageList.add(conCatString);
}
mAdapter.notifyDataSetChanged();
}
}
和
package com.example.t.receivesms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSManager extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
Intent smsIntent=new Intent(context,MainActivity.class);
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
smsIntent.putExtra("MessageNumber", messages.getOriginatingAddress());
smsIntent.putExtra("Message", messages.getMessageBody());
context.startActivity(smsIntent);
Toast.makeText(context, "You have a new message", Toast.LENGTH_LONG).show();
}
}
如果有帮助的话;
logcat的:
12-03 21:34:55.437 15863-15863/? I/art: Late-enabling -Xcheck:jni
12-03 21:34:55.741 15863-15863/com.example.t.receivesms I/InstantRun: starting instant run server: is main process
12-03 21:34:55.791 15863-15863/com.example.t.receivesms V/TelephonyAutoProfiling: [loadFeatureFromXml] *** start feature loading from xml
12-03 21:34:55.791 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [parse] Load xml
12-03 21:34:55.794 15863-15863/com.example.t.receivesms V/TelephonyAutoProfiling: [getMatchedProfile] selected file : /cust/OPEN_EU/config/featureset.xml
12-03 21:34:55.794 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : handle8bit, value : true
12-03 21:34:55.794 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : ConcatMTCheckTimestamp, value : true
12-03 21:34:55.794 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : allow_sending_empty_sms, value : true
12-03 21:34:55.794 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : retry_to_enable_cb, value : true
12-03 21:34:55.794 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : copy_submit_to_uicc, value : true
12-03 21:34:55.794 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : spam, value : true
12-03 21:34:55.795 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : MANUAL_SELECTION_WITH_RAT, value : true
12-03 21:34:55.795 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : SUPPORT_LOG_RF_INFO, value : true
12-03 21:34:55.795 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : seperate_processing_sms_uicc, value : true
12-03 21:34:55.795 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : KRWapPushWithSpam, value : true
12-03 21:34:55.795 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : GLOBALspam, value : true
12-03 21:34:55.795 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : support_emoji_in_concat_message, value : true
12-03 21:34:55.795 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : KSC5601Decoding, value : true
12-03 21:34:55.795 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : KR_Modem_Item, value : true
12-03 21:34:55.795 15863-15863/com.example.t.receivesms D/TelephonyAutoProfiling: [profileToMap] add - key : OperatorMessage, value : true
12-03 21:34:55.795 15863-15863/com.example.t.receivesms V/TelephonyAutoProfiling: [loadFeatureFromXml] load feature from xml complete : {handle8bit=true, ConcatMTCheckTimestamp=true, retry_to_enable_cb=true, allow_sending_empty_sms=true, copy_submit_to_uicc=true, spam=true, MANUAL_SELECTION_WITH_RAT=true, seperate_processing_sms_uicc=true, SUPPORT_LOG_RF_INFO=true, KRWapPushWithSpam=true, GLOBALspam=true, KSC5601Decoding=true, support_emoji_in_concat_message=true, KR_Modem_Item=true, OperatorMessage=true}
12-03 21:34:55.808 15863-15863/com.example.t.receivesms I/Timeline: Timeline: Activity_launch_request id:com.example.t.receivesms time:150316365
12-03 21:34:56.029 15863-15863/com.example.t.receivesms D/ContextHelper: convertTheme. context->name=com.example.t.receivesms themeResourceId=2131492869
12-03 21:34:56.082 15863-15863/com.example.t.receivesms W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-03 21:34:56.133 15863-15863/com.example.t.receivesms I/PhoneWindow: [generateLayout] setColorNavigationBar => color=0x ff000001
12-03 21:34:56.180 15863-15863/com.example.t.receivesms D/PhoneWindowEx: [LMJ][PWEx][generateLayout] setNavigationBarColor2 : colors=0xfff5f5f5
12-03 21:34:56.180 15863-15863/com.example.t.receivesms I/PhoneWindow: [setNavigationBarColor2] color=0x fff5f5f5
12-03 21:34:56.565 15863-15889/com.example.t.receivesms D/OpenGLRenderer: Render dirty regions requested: true
12-03 21:34:56.566 15863-15889/com.example.t.receivesms I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: ()
OpenGL ES Shader Compiler Version: E031.25.03.00
Build Date: 12/12/14 금
Local Branch: LA.BF.1.1_RB1.05.00.00.002.031_20141212_01821442
Remote Branch:
Local Patches:
Reconstruct Branch:
12-03 21:34:56.567 15863-15889/com.example.t.receivesms I/OpenGLRenderer: Initialized EGL, version 1.4
12-03 21:34:56.589 15863-15889/com.example.t.receivesms D/OpenGLRenderer: Enabling debug mode 0
12-03 21:34:56.602 15863-15863/com.example.t.receivesms D/Atlas: Validating map...
12-03 21:34:56.848 15863-15863/com.example.t.receivesms W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
12-03 21:34:56.923 15863-15863/com.example.t.receivesms I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@24e2b0b3 time:150317479
12-03 21:35:03.132 15863-15863/com.example.t.receivesms I/Timeline: Timeline: Activity_launch_request id:com.example.t.receivesms time:150323689
12-03 21:35:03.542 15863-15863/com.example.t.receivesms I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@24e2b0b3 time:150324098
12-03 21:35:11.576 15863-15863/com.example.t.receivesms I/Timeline: Timeline: Activity_launch_request id:com.example.t.receivesms time:150332132
12-03 21:35:11.823 15863-15863/com.example.t.receivesms I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@24e2b0b3 time:150332379
12-03 21:35:21.787 15863-15863/com.example.t.receivesms I/Timeline: Timeline: Activity_launch_request id:com.example.t.receivesms time:150342343
12-03 21:35:22.096 15863-15863/com.example.t.receivesms I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@24e2b0b3 time:150342653
有谁知道为什么会这样,以及如何更改应用程序?
答案 0 :(得分:1)
根据您的代码示例,我可以看到您只是在通过BroadcastReceiver到达时列出短信。
所以这就是发生的事情。
收到短信&gt; BroadcastReceiver接收短信,然后您调用显示该消息的活动。
它的作用是您只在邮件到达时列出邮件,但不会保留列表。当您的应用程序处于活动状态时,该列表仍处于活动状态,因此当它被系统杀死时,或者在下次打开它时强制关闭它,将重新创建列表。而且你的数据丢失了。
解决此问题的一种方法是通过SQLite或SharedPreference将这些短信保存在您自己的存储上。 如果您想真正获得所有短信,您可以使用ContentResolver并在手机中获取短信。
使用内容解析器将SMS发送到列表中的方法如下所示:
public ArrayList<Sms> getAllSms() {
ArrayList<Sms> lstSms = new ArrayList<Sms>();
Sms objSms = new Sms();
Uri message = Uri.parse("content://sms/");
ContentResolver cr = mcontext.getContentResolver();
Cursor c = cr.query(message, null, null, null, null);
mcontext.startManagingCursor(c);
totalSMS = c.getCount();
if (c.moveToFirst()) {
for (int i = 0; i < totalSMS; i++) {
objSms = new Sms();
objSms.displayName=c.getString(c.getColumnIndexOrThrow("_id"));
objSms.address=c.getString(c
.getColumnIndexOrThrow("address"));
objSms.msg=c.getString(c.getColumnIndexOrThrow("body"));
objSms.threadId=c.getString(c.getColumnIndex("read"));
objSms.date=c.getString(c.getColumnIndexOrThrow("date"));
if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
objSms.type="inbox";
} else {
objSms.type="sent";
}
lstSms.add(objSms);
c.moveToNext();
}
}
c.close();
return lstSms;
}