2010-09-20 106 views
3

我想覆盖默认的android消息传递应用程序。 如果我收到短信或彩信,我希望将其发送给电子邮件,但我不想在电话上发送任何通知。 所以基本上我想更换默认的消息应用程序。覆盖默认的android消息传递应用程序

如何让我的应用程序成为接收短信​​的默认应用程序?


非常感谢。这正是我需要的。但我还有一个问题。 我使用接收器来获取消息......但我不知道如何在手机中查找消息并将其标记为已读。

public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    //---get the SMS message passed in--- 
    Bundle bundle = intent.getExtras();   
    SmsMessage[] msgs = null; 
    String str = "";    
    if (bundle != null) 
    { 
     //---retrieve the SMS message received--- 
     Object[] pdus = (Object[]) bundle.get("pdus"); 
     msgs = new SmsMessage[pdus.length];    
     for (int i=0; i<msgs.length; i++){ 
      msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);     
      str += "SMS from " + msgs[i].getOriginatingAddress();      
      str += " :"; 
      str += msgs[i].getMessageBody().toString(); 
      str += "\n";   
     } 
     //---display the new SMS message--- 
     Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 

     //---find and mark the messages as read--- 
     Uri uriSms = Uri.parse("content://sms/inbox/"); 
     try{ 
     Cursor c = context.getContentResolver().query(uriSms, null,null,null,null); 
      //---code to find the message by body and sender--- 
      ... 

    } 

有没有什么方法可以识别消息就像一个ID? 现在我发现邮件比较收件箱中所有邮件的bofy和发件人号码。

感谢, 拉杜

回答

8

没有在你的思维方式的“默认应用程序”。应用程序在Android中分派的方式是通过Intents。应用程序将使用IntentFilter来确定它可以处理特定类型的Intents。您正在寻找的是可以处理SMSReceived意图的BroadcastReceiver。这将允许您的应用程序在收到SMS时收到通知。为了隐藏通知,您需要使用SMS ContentProvider将短信标记为已读。这将清除通知托盘中的通知。除非您从SMS中删除邮件ContentProvider,否则无法隐藏默认邮件应用程序的邮件。查看this link以了解如何开始使用BroadcastReceivers

+0

链接不再可用... – 2012-04-26 17:27:26

+0

@MartinBories感谢您的支持!我用一个正在工作的人取而代之。 – 2012-04-27 13:42:47

0

有一种方法可以阻止消息进入默认消息应用程序。如果您将BroadcastReceiver的优先级设置得足够高(我们使用100),并调用abortBroadcast来阻止任何优先级较低的BroadcastReceivers获取目标。它只有在意图广播被订购时才有效,这是我们无法控制的。但事实证明,SMS_SENT广播是有序的,我怀疑MMS_SENT广播也是如此。

0

如果您发送的BroadcastReceiver的优先级已经足够高,那么您的应用程序是第一个接收到该消息的消息,之前消息存储在数据库中。 我想解决的办法是将消息存储在数据库中,并致电abortBroadcast();,但我没有自己尝试。

祝你好运!

相关问题