0

의해 전송 된 방송을 수신하는 데 실패한다.브로드 캐스트 리시버의 onReceive() 내가로부터 <code>Service</code>가 다른 브로드 캐스트 리시버

private Notification getNotificationAU() { 
     mBuilder = 
       new NotificationCompat.Builder(getBaseContext()) 
         .setSmallIcon(R.mipmap.app_icon_1) 
         .setContentTitle("title") 
         .setContentText(***); 

     // for action button 
     Intent actionIntent = new Intent(getBaseContext(), BroadcastSender.class); 
     PendingIntent actionPendingIntent = PendingIntent 
       .getBroadcast(getBaseContext(), 
         0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     mBuilder.setAutoCancel(true); 
     mBuilder.addAction(***, "***", actionPendingIntent); 
     return mBuilder.build(); 
    } 

여기 BroadcastSender.class 코드입니다 :

public class BroadcastSender extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     Toast.makeText(context, "Broadcast Received by BroadcastSender", Toast.LENGTH_SHORT).show(); 

     // send back to your class 
     Intent newIntent = new Intent(); 
     newIntent.setAction(context.getString(R.string.broadcast_id)); 
     context.sendBroadcast(newIntent); 
     context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); 
     Toast.makeText(context, "Broadcast sent back.", Toast.LENGTH_SHORT).show(); 

    } 
} 

모두Toasts이 위지고

여기에 코드입니다.

다음은 Service 내부 BroadcastReceiver있어 :

여기
public class MyBroadcastReceiver extends BroadcastReceiver { 

     public MyBroadcastReceiver(){ 
      super(); 
     } 

     @Override public void onReceive(Context context, Intent intent) { 

      Toast.makeText(context, "onReceive() in service called", Toast.LENGTH_SHORT).show(); 

     if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id_2))) { 
       ... 
       ... 
       ... 
     } else if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id))) { 

       // perform the task 

     } else { 
       Toast.makeText(context, "Intent is null.", Toast.LENGTH_SHORT).show(); 
     } 
     } 
    } 

strings.xml입니다 :

<string name="broadcast_id">***</string> 

여기 AndroidManifest.xml입니다 :

<receiver android:name=".BroadcastSender"/> 

문제는 것을입니다은 둘 다 동일한 브로드 캐스트 ID R.string.broadcast_id을 가지고 있어도 브로드 캐스트를 수신하지 않습니다.

BroadcastSender.class 님이 보낸 브로드 캐스트를 수신하지 않는 이유는 무엇입니까?

알려 주시기 바랍니다.

+1

브로드 캐스트 리시버를 등록하고 등록을 취소하는 방법은 무엇입니까? 해당 코드 –

+1

을 보여줄 수 있습니까? MyBroadcastReceiver를 등록한 코드를 붙여 넣을 수도 있습니까? –

+0

바보 같은 실수를 찾아 주셔서 감사합니다! 브로드 캐스트 리시버를 등록하는 동안 myIntentFilter.addAction (getString (R.string.broadcast_id));을 추가하는 것을 잊었습니다. 이제 작동합니다! –

답변

0

여기에서 문제는 BroadcastReceiver을 등록하는 동안 myIntentFilter.addAction(getString(R.string.broadcast_id)); 행을 추가하지 않는 것이 었습니다.

줄을 추가 한 후 문제가 해결되었습니다.

이 작지만 중대한 실수를 찾는 것을 도와 준 덕분에 Jitesh MohiteHasif Seyd에게 감사드립니다.