2013-08-20 2 views
1

다음과 같은 목록 항목이있는 앱이 있습니다.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; 
    } 
} 
+0

코드가 좋아 보인다. 디버그 모드에서 앱을 실행하고 onReceive() 메소드에 중단 점을 넣으려고 했습니까? –

+1

@beworker 미안하지만 그렇게 할 수는 없습니다. 앱이 실행 중일 때 제대로 작동합니다. 앱을 닫으면 디버거가 끊어집니다. 이 문제를 재현하려면 방송을 수신 할 때 앱을 시작해야합니다. 디버거는 자동으로 다시 연결되지 않습니다. – AppleGrew

답변

0

나는 방금 코드를 시도했습니다. 잘 작동한다. onReceive()이 호출되지만 Application 열은 LogCat에 채워지지 않습니다. 필터가 설정되어 있으면 메시지가 표시되지 않습니다. 응용 프로그램 필터를 "tag : pdus"로 바꾸거나 필터를 완전히 제거하십시오. 그러면 출력이 표시됩니다. 또한 Eclipse의 "장치"보기에서 "프로세스 중지"단추를 사용하여 응용 프로그램을 중지하십시오.

+0

사실 내 logcat에 어떤 필터도 사용하고 있지 않습니다. 나는 돌아가서 전체 로그를 체크 아웃했지만 여전히 메시지를 보지 못했다. – AppleGrew

+0

정확한 문제는 무엇인지 모르겠지만 자세한 정보 대신 오류로 기록하면 필터가 설정되어 있지 않아도 메시지를 볼 수 있습니다! – AppleGrew

관련 문제