覆盖默认的 Android 消息应用程序

发布于 2024-09-19 21:02:50 字数 1481 浏览 2 评论 0原文

我想覆盖默认的 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一样识别消息吗? 现在我找到了比较收件箱中所有邮件的发件人号码和发件人号码的邮件。

谢谢, 拉杜

I want to override the default android messaging application.
If I receive a sms or mms I want to send that to email but i don't want any notification on phone.
So basically I want to replace the default messaging application.

How can I make my application the default one that receive the sms?


Thanks a lot. That is exactly what I need. But I have one more problem.
I used the receiver to get the message ... but I don't know how to find the message in the phone and mark it as read.

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---
            ...

    }

Is there any way in which I can identify the message like an id?
Now I find the message comparing the bofy and sender number for all messages in inbox.

Thanks,
Radu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

地狱即天堂 2024-09-26 21:02:50

您所想的方式并不存在“默认应用程序”。 Android 中应用程序的调度方式是通过Intents。应用程序将使用IntentFilter来标识它可以处理特定类型的Intents。您正在寻找的是一个可以处理 SMSReceived 意图的 BroadcastReceiver 。这将使您的应用程序在收到短信时收到通知。为了隐藏通知,您需要使用 SMS ContentProvider 将 SMS 标记为已读。这将从通知托盘中清除通知。除非您从 SMS ContentProvider 中删除消息,否则无法隐藏默认消息应用程序中的消息。查看此链接了解如何开始使用BroadcastReceivers.

There isn't a "default application" in the way you're thinking. The way applications are dispatched in Android is through Intents. An application wil use an IntentFilter to identify that it can handle specific types of Intents. What you're looking for is a BroadcastReceiver that can handle the SMSReceived intent. That will allow your application to be notified when an SMS is received. In order to hide the notification, you will need to mark the SMS as read using the SMS ContentProvider. That will clear the notification out of the notification tray. There is no way to hide a message from the default messaging application unless you delete the message from the SMS ContentProvider. Check out this link for how to get started with BroadcastReceivers.

书信已泛黄 2024-09-26 21:02:50

有一种方法可以阻止消息发送到默认消息应用程序。如果您将 BroadcastReceiver 的优先级设置得足够高(我们使用 100)并调用 abortBroadcast 来阻止任何较低优先级的 BroadcastReceiver 接收意图。只有当意图广播被命令时它才会起作用,这是我们无法控制的。但事实证明,SMS_SENT 广播是有序的,我怀疑 MMS_SENT 广播也是如此。

There is a way to block the message from going to the the default messaging app. If you set the priority of your BroadcastReceiver up high enough (we use 100) and call abortBroadcast to block the intent from being picked up by any lower priority BroadcastReceivers. It will only work if the intent broadcast is ordered, something that is out of our control. But it turns out that the SMS_SENT broadcast is ordered, and I suspect the MMS_SENT broadcast is as well.

笑红尘 2024-09-26 21:02:50

如果您发送的 BroadcastReceiver 的优先级足够高,您的应用程序将是第一个接收消息的应用程序,消息存储在数据库中。
我想解决方案是自己将消息存储在数据库中并调用 abortBroadcast(); ,但我自己还没有尝试过。

祝你好运!

If you sent priority of your BroadcastReceiver high enough, your application is the first one receiving the message, BEFORE the message is stored in the database.
I guess the solution would be to store the message in the database yourself and call abortBroadcast();, but I didn't try this myself, yet.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文