2015-04-23 155 views
0

我正在構建一個未讀的短信中繼應用程序。有電話A和B,當A收到短信但沒有人閱讀它時,我的應用程序將它轉發給B.但我發現當A收到短信時,系統會顯示一個通知。我的ContentObserveronChange()方法只有在我消失通知後纔會調用。接收郵件時應該怎樣才能獲取未讀短信?如何在收到短信時獲取未讀短信?

ContentObserver:

public NewIncomingContentObserver(Handler handler, Application 

application) { 
    super(handler); 
    this.mApplication = application; 
    } 

    @Override 
    public void onChange(boolean selfChange) { 
    System.out.println(selfChange); 
    super.onChange(selfChange); 
    Uri uri = Uri.parse(SMS_URI_INBOX); 
    mMessageListener.OnReceived(this.getSmsInfo(uri, mApplication)); 
    } 

    /** 
    * get the newest SMS 
    */ 
    private SmsInfo getSmsInfo(Uri uri, Application application) { 
    ... 
    } 

    public interface MessageListener { 
    public void OnReceived(SmsInfo smsInfo); 
    } 

    public void setOnReceivedMessageListener(
      MessageListener messageListener) { 
    this.mMessageListener = messageListener; 
    } 
} 

服務:

public class SmsListenerService extends Service { 
    public static final String URI = "content://sms/inbox"; 

    public SmsListenerService() { 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
    throw new UnsupportedOperationException("Not yet implemented"); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
    //Register observer 
    NewIncomingContentObserver smsContentObserver = 
      new NewIncomingContentObserver(new Handler(), getApplication()); 

    this.getContentResolver().registerContentObserver 
      (Uri.parse(URI), true, smsContentObserver); 

    smsContentObserver.setOnReceivedMessageListener(new NewIncomingContentObserver.MessageListener() { 
     @Override 
     public void OnReceived(SmsInfo smsInfo) { 
     System.out.println(smsInfo); 
     } 
    }); 
    return START_NOT_STICKY; 
    } 
} 

回答

0

從(https://github.com/Pyo25/Phonegap-SMS-reception-plugin)借來的例子是PhoneGap的插件,但你仍然可以使用它的一個例子:

public class SmsReceiver extends BroadcastReceiver { 
    public static final String SMS_EXTRA_NAME = "pdus"; 

    // This broadcast boolean is used to continue or not the message broadcast 
    // to the other BroadcastReceivers waiting for an incoming SMS (like the native SMS app) 
    private boolean broadcast = false; 

    @Override 
    public void onReceive(Context ctx, Intent intent) { 

     // Get the SMS map from Intent 
     Bundle extras = intent.getExtras(); 
     if (extras != null) 
     { 
      // Get received SMS Array 
      Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME); 

      for (int i=0; i < smsExtra.length; i++) 
      { 
       SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]); 

        String formattedMsg = sms.getOriginatingAddress() + ">" + sms.getMessageBody(); 

      } 

      // If the plugin is active and we don't want to broadcast to other receivers 
      if (!broadcast) { 
       this.abortBroadcast(); 
      } 
     } 
    } 
} 

而在我的AndroidManifest中:

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

是BroadcastReceiver更好的方法嗎?我發現了三種獲取SMS的方法:BroadcastReceiver,ContentObserver和Loader。但我不知道哪一個更有效。 – AngryYogurt

0

SMS Inbox not refereshing

我覺得短信收件箱是由特定的SMS應用程序管理。因此,通過content://sms/inbox收聽新短信是我的應用程序中特定於SMS-app的解決方案。尼爾斯的回答可能是更好的解決方案。