1

我有一个包含以下清单条目的应用程序。

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" android:windowSoftInputMode="adjustPan" package="com.example.app" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />

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

    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17" />
    <application android:allowBackup="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="TestSmsApp" android:theme="@android:style/Theme.Black.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name="com.applegrew.cordova.android.plugin.SmsReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter> 
        </receiver>
    </application>
</manifest>

当我的应用程序停止并收到新短信时,我在 logcat 中看到以下内容。

I/ActivityManager(  540): Start proc com.example.app for broadcast com.example.app/com.applegrew.cordova.android.plugin.SmsReceiver: pid=25457 uid=10095 gids={50095, 1028}
E/Trace   (25457): error opening trace file: No such file or directory (2)

之后,我看不到接收器的更多活动。我还有一个记录器可以在开始时打印消息,onReceive()但我想这从未被调用过。我的接收器代码如下。

package com.applegrew.cordova.android.plugin;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.LOG;
import org.apache.cordova.PluginResult;
import org.json.JSONException;
import org.json.JSONObject;

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

public class SmsReceiver extends BroadcastReceiver {


    public static final String SMS_EXTRA_NAME = "pdus";

    private CallbackContext callback_receive;
    private boolean isReceiving = true;

    // This broadcast boolean is used to continue or not the message broadcast
    // to the other BroadcastReceivers waiting for an incoming SMS (like the native SMS app)
    private boolean broadcast = true;

    @Override
    public void onReceive(Context ctx, Intent intent) {
LOG.v("com.example.app", ">>>>>>>>>>>>>>>>>onReceive called!!!!");
        // Get the SMS map from Intent
        Bundle extras = intent.getExtras();
        if (extras != null)
        {
           // Get received SMS Array
            Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);

            for (int i=0; i < smsExtra.length; i++)
            {
                SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
                if(this.isReceiving && this.callback_receive != null) {
                    JSONObject obj = new JSONObject();
                    try {
                        obj.put(SMS.ADDRESS, sms.getOriginatingAddress());
                        obj.put(SMS.BODY, sms.getMessageBody());
LOG.v("com.example.app", ">>>>>>>>>>>>>>>>>with value" + sms.getOriginatingAddress() + ";; " + sms.getMessageBody());
                        PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
                        result.setKeepCallback(true);
                        callback_receive.sendPluginResult(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            // If the plugin is active and we don't want to broadcast to other receivers
            // Also we cannot abort a broadcast which is not ordered.
            if (this.isReceiving && !broadcast && isOrderedBroadcast()) {
                this.abortBroadcast();
            }
         }
    }

    public void broadcast(boolean v) {
        this.broadcast = v;
    }

    public void startReceiving(CallbackContext ctx) {
        this.callback_receive = ctx;
        this.isReceiving = true;
    }

    public void stopReceiving() {
        this.callback_receive = null;
        this.isReceiving = false;
    }
}
4

1 回答 1

0

我刚刚试过你的代码。它运作良好。onReceive()被调用,但 LogCat 中未填写 Application 列。如果您设置了过滤器,您将不会看到该消息。请用“tag:pdus”替换应用程序过滤器(或完全删除过滤器),您将看到输出。还可以使用 Eclipse 的“设备”视图中的“停止进程”按钮来停止应用程序。

于 2013-08-20T05:47:49.570 回答