다음과 같은 목록 항목이있는 앱이 있습니다.BroadcastReceiver가 시작되지 않는 이유는 무엇입니까?
내 앱이 중지되고 새 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;
}
}
코드가 좋아 보인다. 디버그 모드에서 앱을 실행하고 onReceive() 메소드에 중단 점을 넣으려고 했습니까? –
@beworker 미안하지만 그렇게 할 수는 없습니다. 앱이 실행 중일 때 제대로 작동합니다. 앱을 닫으면 디버거가 끊어집니다. 이 문제를 재현하려면 방송을 수신 할 때 앱을 시작해야합니다. 디버거는 자동으로 다시 연결되지 않습니다. – AppleGrew