在Android中监控传入的SMS消息

时间:2012-09-05 02:48:45

标签: android sms

我有这个程序会尝试监控传入的短信。但是当我试图在模拟器中运行我的程序并尝试发送短信时,它可以工作。但是当我在手机中安装我的程序时,它无法正常工作。

有什么问题?

我也希望在后端运行该程序。怎么做?

BTW,以下是此示例应用的完整代码。

由于

RJ


import java.io.BufferedWriter;
import java.io.FileWriter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class MySMSMonitor extends BroadcastReceiver
{
    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

    @Override 
    public void onReceive(Context context, Intent intent) 
    {
        if(intent!=null && 
                intent.getAction()!=null && 
                ACTION.compareToIgnoreCase(intent.getAction())==0)
        {
            Object[]pduArray= (Object[]) intent.getExtras().get("pdus");
            SmsMessage[] messages = new SmsMessage[pduArray.length];

            for (int i = 0; i<pduArray.length; i++) {
                messages[i] = SmsMessage.createFromPdu ((byte[])pduArray [i]); 
            }
            Log.d("MySMSMonitor","SMS Message Received.");
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="spy.Frandy.com"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="6" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TelephonyDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

    </application>
</manifest>

import android.app.Activity; 
import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast;

public class TelephonyDemo extends Activity
{
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main); 
        Button sendBtn = (Button)findViewById(R.id.sendSmsBtn);

        sendBtn.setOnClickListener(new OnClickListener(){
            @Override public void onClick(View view) {
            EditText addrTxt = (EditText)TelephonyDemo.this.findViewById(R.id.addrEditText);

            EditText msgTxt = (EditText)TelephonyDemo.this.findViewById(R.id.msgEditText);
            try { 
                sendSmsMessage(
                        addrTxt.getText().toString(),msgTxt.getText().toString()); 
                Toast.makeText(TelephonyDemo.this, "SMS Sent",
                        Toast.LENGTH_LONG).show(); 
            } catch (Exception e) {
                Toast.makeText(TelephonyDemo.this, "Failed to send SMS", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }});
    }

    @Override 
    protected void onDestroy() {
        super.onDestroy();
    }

    private void sendSmsMessage(String address,String message)throws Exception {
        SmsManager smsMgr = SmsManager.getDefault(); 
        smsMgr.sendTextMessage(address, null, message, null, null);
    }
}

3 个答案:

答案 0 :(得分:1)

我将假设你提供的小细节,它在模拟器而不是你的实际手机上工作的原因;可能是另一个有冲突的应用在广播到您的应用之前截取这些消息。

尝试在应用清单中将android:priority="1000"添加到接收者的intent-filter中 (一千个你认为必要的数字,如果需要可以更高)

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

更多信息 - intent-filter priority

答案 1 :(得分:1)

我只是坚持要您使用ContentObserver并注册content://sms Uri并使用

获取短信的type
cusor.getString(cusor.getColumnIndex("type")); // 1 = SMS Received 
                                               // 2 =  SMS Sent

此外,您可以轻松地从您获取的光标中获取所需的数据。 Here也是一个博客。

答案 2 :(得分:0)

试试这个:

<receiver android:name=".MySMSMonitor"
              android:permission="android.permission.BROADCAST_SMS">

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