处理传入的消息我的broadCastReceiver不起作用

时间:2012-05-04 20:19:25

标签: android broadcastreceiver

大家好,我有非常重要的问题,请帮助我。 我正在尝试使用broadcastReceiver处理传入的消息。但是,它不起作用。我发送以下代码请帮助我。

我的宣言:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.commonsware.andorid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET"/>
      <uses-permission android:name="android.permission.READ_CONTACTS" />

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



    <application
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
         <receiver android:name=".SMSBroadcastReceiver">
           <intent-filter android:priority="999">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>
        <activity
            android:name=".NewPage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NewMessage" />
        <activity android:name=".SignFrame"></activity>
        <activity android:name=".MyMenu"></activity>
        <activity android:name=".Detail"></activity>
        <activity android:name=".Browser"></activity>
        <activity android:name=".Login"></activity>
        <activity android:name=".UserDetail"></activity>
        <activity android:name=".AndroidAppActivity"></activity>
    </application>

</manifest>




My Class:


package com.commonsware.andorid;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.acl.Owner;
import java.util.StringTokenizer;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.SmsMessage;

import android.util.Log;
import android.widget.Toast;

public class SMSBroadcastReceiver extends BroadcastReceiver {




        private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
        private static final String TAG = "SMSBroadcastReceiver";
        RSA rsa;
        DataBaseHelper db;
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                rsa=new RSA();
            } catch (InvalidKeyException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (NoSuchAlgorithmException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IllegalBlockSizeException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (NoSuchProviderException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (BadPaddingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (NoSuchPaddingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
             Log.i(TAG, "Intent recieved: " + intent.getAction());

             if (intent.getAction().equals(SMS_RECEIVED)) {
                abortBroadcast();
                    Bundle bundle = intent.getExtras();
                    if (bundle != null) {
                        Object[] pdus = (Object[])bundle.get("pdus");
                        final SmsMessage[] messages = new SmsMessage[pdus.length];
                        for (int i = 0; i < pdus.length; i++) {
                            messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        }
                        if (messages.length > -1) {
                            abortBroadcast();
                            Toast toast =Toast.makeText(context, messages.toString(), Toast.LENGTH_LONG);
                            String body = messages[0].getMessageBody().toString();
                            String address = messages[0].getOriginatingAddress();
                            System.out.println(address);
                            String decrypted = null;

                            toast.show();
                            Intent i = new Intent(context, Detail.class);
                            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(i); 
                            try {
                                decrypted=rsa.decrypt(body.getBytes());

                            } catch (InvalidKeyException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IllegalBlockSizeException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (BadPaddingException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (NoSuchAlgorithmException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (NoSuchPaddingException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
                            if(body.startsWith("<publicKey>")){
                                createContact(messages[0]);

                            }
                            else if(body.startsWith("<secureSmsMessaging>")){
                            createMessage(messages[0]);
                            }
                            messages[0].getMessageBody().replaceAll(body, decrypted);


                        }
                    }
                }

                this.abortBroadcast();
           }
}

我试图调试它。但它没有进入我的广播接收器。每当我发送消息时,Android的默认消息服务就会处理它。我做错了什么?

我在等你的回答。谢谢。

Sevil。

2 个答案:

答案 0 :(得分:2)

我猜你错了@ onReceive方法。

我创建了一个小应用程序来使用广播接收器监听传入消息。

SMSBroadcastReceiver.java

public class SMSBroadcastReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    if(bundle != null){

        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 Phone No: " +msgs[i].getOriginatingAddress();
            str +="\n"+"Message is: ";
            str += msgs[i].getMessageBody().toString();
            str +="\n";
        }

        Log.v("Debug", str);
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }
}}

的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.rdc"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

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

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <receiver android:name=".SMSBroadcastReceiver">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>

</application></manifest>

你可以检查一下并实现你的代码!!

答案 1 :(得分:2)

我通过使用https服务器解决了我的问题。我的问题是在发送用户的公钥和加密消息时将包含字节数组的字符串转换为字节数组。现在我正在使用https服务器编写我的员工和然后从那里阅读。它现在更经济。我建议每个人都使用https服务器谁想要在消息传递中提供加密,因为你可能会遇到很多问题。