ANDROID:广播接收器连续接收相同的消息

时间:2013-11-26 14:52:06

标签: java android android-intent

我在android中创建了一个应用程序,它通过文件列表创建TAR文件。

在MainActivity中,我将文件列表发送到服务:TarCreatorService。 TarCreatorService创建TAR文件并通过内部广播通知MainActivity。 这个通知收到了很多次,我不知道为什么。

我有断点,其中所有意图都被发送,并且它们只被触发一次,但是来自TarCreatorService和MainActivity的消息被MainActivity的BroadcastReceiver连续接收。

以下是代码:

MainActivity:将Intent发送到TarCreatorService

private void Tar(List<File> files,String EID)
    {
        Intent i=new Intent(this,TarCreatorService.class);
        i.setAction(TarCreatorService.ACTION_TAR_FILE_LIST);
        i.putExtra(TarCreatorService.EXTRA_SOURCE, (Serializable)Utils.getDtnBoxStorageDir());
        i.putExtra(TarCreatorService.EXTRA_SOURCE_FILES, (Serializable)files);
        i.putExtra(TarCreatorService.EXTRA_DESTINATION, (Serializable)this.getFilesDir());
        i.putExtra(FileHandleService.EXTRA_LOCAL_BROADCAST_DESYNC_FILES_EID, EID);
        startService(i);
    }

TarCreatorService:从MainActivity接收Intent并将Intent发送给MainActivity

    public TarCreatorService() {super(TAG);}


@Override
protected void onHandleIntent(Intent intent) {


    if(ACTION_TAR_FILE_LIST.equals(intent.getAction()))
    {
        String EID = intent.getStringExtra(FileHandleService.EXTRA_LOCAL_BROADCAST_DESYNC_FILES_EID);

        List<File> sourceFiles = (List<File>)intent.getSerializableExtra(EXTRA_SOURCE_FILES);
        File source = (File)intent.getSerializableExtra(EXTRA_SOURCE);
        File destination = (File)intent.getSerializableExtra(EXTRA_DESTINATION);
        File tar= null;
        Intent i =new Intent(ACTION_LOCAL_BROADCAST_TARCREATORSERVICE);
        try{
            tar=TAR.PackFiles(sourceFiles,source, destination);
            i.putExtra(EXTRA_LOCAL_BROADCAST_WHAT, LOCAL_BROADCAST_TAR);
            i.putExtra(EXTRA_LOCAL_BROADCAST_TAR, (Serializable)tar);
            i.putExtra(FileHandleService.EXTRA_LOCAL_BROADCAST_DESYNC_FILES_EID,EID);
            LocalBroadcastManager.getInstance(this).sendBroadcast(i);
        }
        catch(Exception e)
        {
            i.putExtra(FileHandleService.EXTRA_LOCAL_BROADCAST_DESYNC_FILES_EID,EID);
            i.putExtra(EXTRA_LOCAL_BROADCAST_WHAT, LOCAL_BROADCAST_TAR_ERROR);
            i.putExtra(EXTRA_LOCAL_BROADCAST_TAR_ERROR, (Serializable)e);
            LocalBroadcastManager.getInstance(this).sendBroadcast(i);
        };
    }

}

MainActivity:通过内部BroadcastReceiver

从TarCreatorService接收意图
private BroadcastReceiver mBroadcastReceiver= new BroadcastReceiver() {

...

else if(TarCreatorService.ACTION_LOCAL_BROADCAST_TARCREATORSERVICE.equals(intent.getAction()) && intent.hasExtra(TarCreatorService.EXTRA_LOCAL_BROADCAST_WHAT))
                switch(intent.getIntExtra(TarCreatorService.EXTRA_LOCAL_BROADCAST_WHAT, -1))
                {
                    case TarCreatorService.LOCAL_BROADCAST_TAR :
                        String EID= intent.getStringExtra(FileHandleService.EXTRA_LOCAL_BROADCAST_DESYNC_FILES_EID);
                        File tar= (File)intent.getSerializableExtra(TarCreatorService.EXTRA_LOCAL_BROADCAST_TAR);
                        SpawnMessage(tar.getAbsolutePath());




                        break;
                    case TarCreatorService.LOCAL_BROADCAST_TAR_ERROR :
                        Exception e=(Exception)intent.getSerializableExtra(TarCreatorService.EXTRA_LOCAL_BROADCAST_TAR_ERROR);
                        SpawnMessage(e.toString());
                        break;
                }
}

后一段代码被无限触发。

我还在onResume()中注册BroadcastReceiver并在onDestroy()中取消注册

我真的无法解决这个问题

0 个答案:

没有答案