BroadcastReceiver并不总是收到

时间:2017-11-28 09:35:50

标签: java android broadcastreceiver intentservice

我有一个扩展IntentService的课程;在本课程中,我发送一个Intent,该BroadcastReceiver应由ActivityBroadcastReceiver内注册。 问题是Intent有90%的时间正确接收Intent。 10%它没有。 调试我的应用程序我看到我的所有代码都完美无缺,直到发送Intent为止。问题发生在... <receiver android:name=".BroadcastReceiver.MediaBroadcastReceiver" android:enabled="true" android:exported="true" /> ... 发送后!

这是我的代码。 清单文件:

public class ContactDetail extends AppCompatActivity implements MediaBroadcastReceiverInterface{
...
   @Override
   public void MediaBroadcastReceiverCallback() {
      //This is the method that is not always called
   }
...
}

活动中的BroadcastReceiver:

public class MediaBroadcastReceiver extends BroadcastReceiver {

    public interface MediaBroadcastReceiverInterface{
       void MediaBroadcastReceiverCallback();      
    }

    private MediaBroadcastReceiverInterface mediaBroadcastReceiverInterface;    
    public static final String PROCESS_RESPONSE = "com.xxx.appname.BroadcastReceiver.MediaBroadcastReceiver.PROCESS_RESPONSE";

    public MediaBroadcastReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent){            
        try {
            mediaBroadcastReceiverInterface = (MediaBroadcastReceiverInterface)context;   
            mediaBroadcastReceiverInterface.MediaBroadcastReceiverCallback();
        }
        catch (ClassCastException e) {
            Log.i(TAG, "Activity doesn't implement the MediaBroadcastReceiverInterface interface");
        }

    }
}

这是BrodcastReceiver:

public class MediaIntentService extends IntentService {   
    public void send(){            
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction(MediaBroadcastReceiver.PROCESS_RESPONSE);
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);        
        sendBroadcast(broadcastIntent);        
    }
}

这是我将Intent发送到BroadCastReceiver

的IntentService
source 'https://github.com/CocoaPods/Old-Specs.git'

platform :ios, '9.0'
use_frameworks!

xcodeproj 'SF'

#link_with 'SF'

target 'SF' do
    pod 'TransitionKit'
    pod 'MBProgressHUD'
    pod 'PureLayout'
    pod 'UICountingLabel'
    pod 'MWPhotoBrowser', :git => 'https://github.com/pash3r/MWPhotoBrowser.git', :branch => 'skyFlyWork'
end

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

您正在清单中静态注册广播。使用清单中的代码。虽然在你的情况下我建议你应该在运行时注册/取消注册它。

 <receiver
        android:name=".BroadcastReceiver.MediaBroadcastReceiver"
        android:enabled="true"
        android:exported="true" >
    <intent-filter android:priority="1000">
    <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </receiver>

答案 1 :(得分:0)

将广播接收器更改为

public class MediaBroadcastReceiver extends BroadcastReceiver {

    public interface MediaBroadcastReceiverInterface{
       void MediaBroadcastReceiverCallback();      
    }

    private MediaBroadcastReceiverInterface mediaBroadcastReceiverInterface;    
    public static final String PROCESS_RESPONSE = "com.xxx.appname.BroadcastReceiver.MediaBroadcastReceiver.PROCESS_RESPONSE";

    public MediaBroadcastReceiver(MediaBroadcastReceiverInterface media) {
        mediaBroadcastReceiverInterface = media;
    }

    @Override
    public void onReceive(Context context, Intent intent){            
        try {
            mediaBroadcastReceiverInterface.MediaBroadcastReceiverCallback();
        }
        catch (ClassCastException e) {
            Log.i(TAG, "Activity doesn't implement the MediaBroadcastReceiverInterface interface");
        }

    }
}

然后从receiver移除manifest并在Activity注册,如

public class ContactDetail extends AppCompatActivity {
    ...
             @Override
              public void onCreate(Bundle bundle){
               ...
                 registerReceiver(new MediaBroadcastReceiver(new MediaBroadcastReceiverInterface(){
                    @Override
                    public void MediaBroadcastReceiverCallback() {
                      //Will be triggered
                    }
                 },new IntentFilter("actionname");
                 ...
               }
     ...
 }