次のマニフェスト エントリを持つアプリがあります。
<?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>
アプリを停止して新しい SMS を受信すると、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;
}
}