如何读取Android的使用服务在后台收到的消息? [英] How to read the incoming message using service in background in android?

查看:88
本文介绍了如何读取Android的使用服务在后台收到的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发在Android应用程序,我想阅读收到的消息不知道user.I希望有一个新的消息接收方式始终运行在background.If传入邮件检查我想读的内容消息,该消息包含了一些字(密码)意味着我要激活的应用程序

I am developing an android application in that i want to read the incoming message without knowing the user.I want to always run the incoming message checker in background.If a new message is received means i want to read the content of the message and that message contains some words(password) means i want to activate the application

请解释一下我该怎么做样品code,因为我是新来的Andr​​oid版

Please explain me how to do that with sample code because i am new to android

推荐答案

看看BroadCastReceivers您必须实现并注册Reciever为 android.provider.Telephony.SMS_RECEIVED

Take a look at BroadCastReceivers you must implement and register a Reciever for android.provider.Telephony.SMS_RECEIVED

下面是一个code片段,让您阅读邮件送达。

Here is a code snippet that lets you read messages as they arrive.

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 SMSReceiver extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
        Bundle myBundle = intent.getExtras();
        SmsMessage [] messages = null;
        String strMessage = "";

        if (myBundle != null)
        {
            Object [] pdus = (Object[]) myBundle.get("pdus");
            messages = new SmsMessage[pdus.length];

            for (int i = 0; i < messages.length; i++)
            {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                strMessage += "SMS From: " + messages[i].getOriginatingAddress();
                strMessage += " : ";
                strMessage += messages[i].getMessageBody();
                strMessage += "\n";
            }

            Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
        }
    }
}

在这里,你有什么要添加到您的Andr​​oidManifest.xml文件:

And here what you have to add to your AndroidManifest.xml file:

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

<receiver android:name=".SMSReceiver">
    <intent-filter> 
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
    </intent-filter> 
</receiver>

这篇关于如何读取Android的使用服务在后台收到的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆