用于接收SMS的应用程序第一次运行良好,第二次未显示所有消息



我的SMS收录器有一些问题。该应用程序运行时应该第一次运行。只要我不关闭应用程序,所有消息都会显示在我的listView中。

如果我关闭了我的应用程序,那么第二次表现出奇怪。通常只显示第一条消息。当第一个消息"到达"到应用程序时,内置的SMS应用程序不会显示该消息的预览。在第二个消息和向前,显示了内置应用程序中的消息。(我既接收并从同一手机发送消息,但如果从其他电话收到消息,也会发生同样的问题)

如果显示了多个消息,则是第一张消息的副本。

我有两个类;

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

有人知道为什么会发生这种情况,以及如何更改应用程序?

基于您的代码样本,我可以看到您仅在SMS通过Broadcastreceiver到达时列出它们。

所以这就是发生的事情。

接收到的SMS> Broadcastreceiver接收SMS,然后调用显示消息的活动。

它的作用是您仅在消息到达时列出消息,但不坚持您的列表。当您的应用程序还活着时,该列表就还活着,因此,当它被系统杀死时,或者下次打开时强制将其关闭时,将重新创建列表。您的数据丢失了。

解决此问题的一种方法是通过sqlite或sharedPreference将这些SMS保存在您自己的存储中。如果您想真正获取所有SMS,则可以使用ContentResolver并在手机中获取SMS。

一种使用内容解析器进入列表的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;
}

相关内容

  • 没有找到相关文章

最新更新